2010-07-28 185 views
2

我有關於此查詢的問題。我想獲得申請人的最新職位(請參閱下面的查詢)。結果應該是這樣的:SQL Server查詢問題

aId Position    Startdate  Enddate  

154 Web Developer   2008-04-07  NULL 
155 Analyst    2008-06-12  2009-06-12 
156 Quality Controller  2001-08-01  2010-01-01 
165 Programmer    1995-08-02  2010-01-01 
166 Programmer    2001-01-14  2010-01-14 
170 Web Developer   2010-03-17  NULL 
168 Business Analyst  2010-05-10  NULL 

,但下面的查詢是給了我這個樣子

aId Position      Startdate     Enddate  

154 Lead Software Developer 2007-04-07 00:00:00.000 2008-04-07 00:00:00.000 
154 Web Developer 2008-04-07 00:00:00.000 NULL 
155 Analyst 2008-06-12 00:00:00.000 2009-06-12 00:00:00.000 
156 Quality Controller 2001-08-01 00:00:00.000 2010-01-01 00:00:00.000 
165 Programmer 1995-08-02 00:00:00.000 2010-01-01 00:00:00.000 
166 Programmer 2001-01-14 00:00:00.000 2010-01-14 00:00:00.000 
170 Web Developer 2010-03-17 00:00:00.000 NULL 
168 Business Analyst 2010-05-10 00:00:00.000 NULL 
168 Analyst Programmer 2010-05-03 00:00:00.000 2010-05-10 00:00:00.000 

結果查詢不能重複具有相同輔助數據,什麼是與此失蹤下面的查詢?

SELECT 
    aId 
    , Position 
    , StartDate 
    , Enddate 
FROM EmploymentDetails ed 
WHERE 
    ((Enddate IS NULL) OR 
    (Enddate = (SELECT MAX(Enddate) 
       FROM EmploymentDetails edin 
       WHERE edin.aId = ed.aId))) 




    create table EmploymentDetails(
    Id     bigint   not null identity constraint PK_EmploymentDetails primary key, 
    aId     bigint   not null, 
    Startdate   datetime, 
    Enddate    datetime, 
    Position   varchar(30), 
    PositionLevelId  bigint   not null, 
    SpecializationId bigint   not null, 
    PositionId   bigint   not null, 
    StartSalary   varchar(50), 
    EndSalary   varchar(50), 
    DescriptionofDuties nvarchar(1000), 
    ReasonforLeaving nvarchar(200), 
    CompanyName   nvarchar(100), 
    TypeofBusiness  varchar(50), 
    Address1   varchar(25), 
    Address2   varchar(25), 
    City    varchar(25), 
    Province   varchar(25), 
    StateorRegion  varchar(25), 
    CountryId   bigint, 
    PostalCode   varchar(10) 
) 
go 






    INSERT [dbo].[EmploymentDetails] ([Id], [aId], [Startdate], [Enddate], [Position], [PositionLevelId], [SpecializationId], [PositionId], [StartSalary], [EndSalary], [DescriptionofDuties], [ReasonforLeaving], [CompanyName], [TypeofBusiness], [Address1], [Address2], [City], [Province], [StateorRegion], [CountryId], [PostalCode]) VALUES (34, 154, CAST(0x0000962900000000 AS DateTime), CAST(0x0000979F00000000 AS DateTime), N'Sr. .Net Developer', 4, 1, 1, N'P12,000', N'P12,000', N'Design websites for company using Adobe dreamweaver, Flash and other web applications.', N'sample reason for leaving', N'Appsource', N'Information Technology', N'Unit 1401 Robinsons Equit', N'ADB Ave. Corner Poveda Ro', N'Pasig', N'NCR', N'NCR', 177, N'') 
INSERT [dbo].[EmploymentDetails] ([Id], [aId], [Startdate], [Enddate], [Position], [PositionLevelId], [SpecializationId], [PositionId], [StartSalary], [EndSalary], [DescriptionofDuties], [ReasonforLeaving], [CompanyName], [TypeofBusiness], [Address1], [Address2], [City], [Province], [StateorRegion], [CountryId], [PostalCode]) VALUES (35, 154, CAST(0x0000990900000000 AS DateTime), CAST(0x00009A7700000000 AS DateTime), N'Lead Software Developer', 4, 2, 1, N'P12,000', N'P12,000', N'Design websites for company using Adobe dreamweaver, Flash and other web applications.', N'sample reason for leaving', N'Corebuilt Technologies', N'Information Technology', N'24/F 88 Corporate Center', N'Valero St.,', N'Makati', N'NCr', N'NCr', 177, N'') 
INSERT [dbo].[EmploymentDetails] ([Id], [aId], [Startdate], [Enddate], [Position], [PositionLevelId], [SpecializationId], [PositionId], [StartSalary], [EndSalary], [DescriptionofDuties], [ReasonforLeaving], [CompanyName], [TypeofBusiness], [Address1], [Address2], [City], [Province], [StateorRegion], [CountryId], [PostalCode]) VALUES (36, 154, CAST(0x00009A7700000000 AS DateTime), NULL, N'Web Developer', 4, 1, 1, N'P15,000', N'P15,000', N'sample description', N'sample description', N'IDCSI', N'Information Technology', N'15/F Summit One Tower', N'530 Shaw Blvd.,', N'Mandaluyong', N'', N'NCR', 177, N'') 
INSERT [dbo].[EmploymentDetails] ([Id], [aId], [Startdate], [Enddate], [Position], [PositionLevelId], [SpecializationId], [PositionId], [StartSalary], [EndSalary], [DescriptionofDuties], [ReasonforLeaving], [CompanyName], [TypeofBusiness], [Address1], [Address2], [City], [Province], [StateorRegion], [CountryId], [PostalCode]) VALUES (38, 155, CAST(0x00009AB900000000 AS DateTime), CAST(0x00009C2600000000 AS DateTime), N'Analyst', 4, 3, 1, N'P15,000', N'P15,000', N'sample description', N'sample description', N'IDCSI', N'Information Technology', N'15/F Summit One Tower', N'530 Shaw Blvd.,', N'Mandaluyong', NULL, NULL, 177, NULL) 
INSERT [dbo].[EmploymentDetails] ([Id], [aId], [Startdate], [Enddate], [Position], [PositionLevelId], [SpecializationId], [PositionId], [StartSalary], [EndSalary], [DescriptionofDuties], [ReasonforLeaving], [CompanyName], [TypeofBusiness], [Address1], [Address2], [City], [Province], [StateorRegion], [CountryId], [PostalCode]) VALUES (40, 156, CAST(0x000090EE00000000 AS DateTime), CAST(0x00009CF100000000 AS DateTime), N'Quality Controller', 4, 2, 1, N'P15,000', N'P15,000', N'sample description', N'sample description', N'IDCSI', N'Information Technology', N'15/F Summit One Tower', N'530 Shaw Blvd.,', N'Mandaluyong', N'', N'', 177, N'') 
INSERT [dbo].[EmploymentDetails] ([Id], [aId], [Startdate], [Enddate], [Position], [PositionLevelId], [SpecializationId], [PositionId], [StartSalary], [EndSalary], [DescriptionofDuties], [ReasonforLeaving], [CompanyName], [TypeofBusiness], [Address1], [Address2], [City], [Province], [StateorRegion], [CountryId], [PostalCode]) VALUES (41, 165, CAST(0x0000722E00000000 AS DateTime), CAST(0x0000882800000000 AS DateTime), N'Analyst', 4, 2, 1, N'P15,000', N'P15,000', N'sample description', N'sample description', N'Microsoft', N'Information Technology', N'15/F Summit One Tower', N'530 Shaw Blvd.,', N'Mandaluyong', NULL, NULL, 177, NULL) 
INSERT [dbo].[EmploymentDetails] ([Id], [aId], [Startdate], [Enddate], [Position], [PositionLevelId], [SpecializationId], [PositionId], [StartSalary], [EndSalary], [DescriptionofDuties], [ReasonforLeaving], [CompanyName], [TypeofBusiness], [Address1], [Address2], [City], [Province], [StateorRegion], [CountryId], [PostalCode]) VALUES (42, 165, CAST(0x0000885F00000000 AS DateTime), CAST(0x00009CF100000000 AS DateTime), N'Programmer', 4, 3, 1, N'P15,000', N'P15,000', N'sample description', N'sample description', N'Microsoft', N'Information Technology', N'15/F Summit One Tower', N'530 Shaw Blvd.,', N'Mandaluyong', N'', N'', 177, N'') 
INSERT [dbo].[EmploymentDetails] ([Id], [aId], [Startdate], [Enddate], [Position], [PositionLevelId], [SpecializationId], [PositionId], [StartSalary], [EndSalary], [DescriptionofDuties], [ReasonforLeaving], [CompanyName], [TypeofBusiness], [Address1], [Address2], [City], [Province], [StateorRegion], [CountryId], [PostalCode]) VALUES (44, 166, CAST(0x0000902700000000 AS DateTime), CAST(0x00009CFE00000000 AS DateTime), N'Programmer', 4, 1, 1, N'P15,000', N'P15,000', N'sample description', N'sample description', N'Microsoft', N'Information Technology', N'15/F Summit One Tower', N'530 Shaw Blvd.,', N'Mandaluyong', NULL, NULL, 177, NULL) 
INSERT [dbo].[EmploymentDetails] ([Id], [aId], [Startdate], [Enddate], [Position], [PositionLevelId], [SpecializationId], [PositionId], [StartSalary], [EndSalary], [DescriptionofDuties], [ReasonforLeaving], [CompanyName], [TypeofBusiness], [Address1], [Address2], [City], [Province], [StateorRegion], [CountryId], [PostalCode]) VALUES (45, 168, CAST(0x0000887E00000000 AS DateTime), CAST(0x00009B0B00000000 AS DateTime), N'Analyst', 4, 2, 1, N'P15,000', N'P15,000', N'sample description', N'sample description', N'Microsoft', N'Information Technology', N'15/F Summit One Tower', N'530 Shaw Blvd.,', N'Mandaluyong', NULL, NULL, 177, NULL) 
INSERT [dbo].[EmploymentDetails] ([Id], [aId], [Startdate], [Enddate], [Position], [PositionLevelId], [SpecializationId], [PositionId], [StartSalary], [EndSalary], [DescriptionofDuties], [ReasonforLeaving], [CompanyName], [TypeofBusiness], [Address1], [Address2], [City], [Province], [StateorRegion], [CountryId], [PostalCode]) VALUES (47, 170, CAST(0x00009D3C00000000 AS DateTime), NULL, N'Web Developer', 4, 2, 1, N'P15,000', N'P15,000', N'sample description', N'sample description', N'Microsoft', N'Information Technology', N'15/F Summit One Tower', N'530 Shaw Blvd.,', N'Mandaluyong', NULL, NULL, 177, NULL) 
INSERT [dbo].[EmploymentDetails] ([Id], [aId], [Startdate], [Enddate], [Position], [PositionLevelId], [SpecializationId], [PositionId], [StartSalary], [EndSalary], [DescriptionofDuties], [ReasonforLeaving], [CompanyName], [TypeofBusiness], [Address1], [Address2], [City], [Province], [StateorRegion], [CountryId], [PostalCode]) VALUES (48, 156, CAST(0x00009A7300000000 AS DateTime), CAST(0x00009BE500000000 AS DateTime), N'', 4, 2, 1, N'P34,343', N'P3,434', N'fgf', N'fgfggf', N'fgfg', N'Information Technology', N'fgfg', N'fgfg', N'fgfg', N'fgfgf', N'fgfg', 177, N'4545') 
INSERT [dbo].[EmploymentDetails] ([Id], [aId], [Startdate], [Enddate], [Position], [PositionLevelId], [SpecializationId], [PositionId], [StartSalary], [EndSalary], [DescriptionofDuties], [ReasonforLeaving], [CompanyName], [TypeofBusiness], [Address1], [Address2], [City], [Province], [StateorRegion], [CountryId], [PostalCode]) VALUES (49, 156, CAST(0x00009BDF00000000 AS DateTime), CAST(0x00009C4700000000 AS DateTime), N'', 4, 1, 1, N'P343', N'P344,343', N'dfdf', N'dfdf', N'dfdfd', N'Information Technology', N'ff', N'', N'dffd', N'', N'', 177, N'') 
INSERT [dbo].[EmploymentDetails] ([Id], [aId], [Startdate], [Enddate], [Position], [PositionLevelId], [SpecializationId], [PositionId], [StartSalary], [EndSalary], [DescriptionofDuties], [ReasonforLeaving], [CompanyName], [TypeofBusiness], [Address1], [Address2], [City], [Province], [StateorRegion], [CountryId], [PostalCode]) VALUES (51, 168, CAST(0x00009D7200000000 AS DateTime), NULL, N'Business Analyst', 4, 1, 1, N'P3,434', N'P33,333', N'3434', N'3434', N'dsd', N'Information Technology', N'sdsds', N'', N'sdsd', N'', N'', 177, N'') 
INSERT [dbo].[EmploymentDetails] ([Id], [aId], [Startdate], [Enddate], [Position], [PositionLevelId], [SpecializationId], [PositionId], [StartSalary], [EndSalary], [DescriptionofDuties], [ReasonforLeaving], [CompanyName], [TypeofBusiness], [Address1], [Address2], [City], [Province], [StateorRegion], [CountryId], [PostalCode]) VALUES (52, 168, CAST(0x00009D6B00000000 AS DateTime), CAST(0x00009D7200000000 AS DateTime), N'Analyst Programmer', 4, 1, 1, N'P43,434', N'P434,343', N'4', N'344', N'3434', N'Information Technology', N'434', N'', N'3434', N'', N'', 177, N'') 
+0

請格式化您的問題以便讀取。所見即所得的編輯器存在的原因。另外,您可能還沒有提供足夠的數據來回答這個問題 - 表格定義比示例數據更簡單,並且您需要簡單的英語解釋您要選擇的數據。 – Tomalak 2010-07-28 09:11:05

+0

我寧願做一個'開始日期=(SELECT MAX(開始日期)'... – pascal 2010-07-28 09:15:33

回答

1

假設的SQL Server 2005+

WITH E AS 
(
SELECT 
    aId 
    , Position 
    , StartDate 
    , Enddate 
    ,ROW_NUMBER() OVER (PARTITION BY aId ORDER BY 
      CASE WHEN Enddate IS NULL THEN 0 ELSE 1 END ASC, 
      (CAST(Enddate as datetime)) DESC) AS RN 
FROM EmploymentDetails ed 
) 
LatestJob AS (SELECT aId, Position, StartDate, Enddate FROM E WHERE RN=1), 
EarliestStart AS (SELECT ... 

通過你爲什麼存儲日期爲varchar,而不是date(SQL2008)或datetime的方式? 編輯我看到這個最後一個問題現在已經修復在問題中。

+0

我怎樣才能插入您的查詢從這句話 WITH AS LatestJob(此處插入您的查詢) ,EarliestStart AS(SELECT援助 , sum(DATEDIFF(YEAR,Startdate,isnull(Enddate,getdate())))AS YearsExperience FROM EmploymentDetails GROUP BY aId) SELECT distinct u.FirstName +''+ u.LastName AS NAME – user335160 2010-07-28 09:47:37

+0

,lj.Position AS LatestPosition ,年份體驗 ,ad.ExpectedSalary,REPLACE(ISNULL(Address1,'')+','+ ISNULL(Address2,'')+','+ ISNULL(City,''),',',',',' )AS地址 FROM Users JO JOIN LatestJob lj ON u.Id = lj.aid JOIN EarliestStart ye ON ye.aId = u.Id 加入申請人詳細信息廣告開放ad.aId = u.Id – user335160 2010-07-28 09:48:06

+0

好的沒關係,謝謝 – user335160 2010-07-28 09:50:14