Sql server SQL Server查询问题

Sql server SQL Server查询问题,sql-server,tsql,Sql Server,Tsql,关于这个问题,我有一个问题。我想获得申请人的最新职位,请参见下面的查询。结果应该是这样的: aId Position Startdate Enddate 154 Web Developer 2008-04-07 NULL 155 Analyst 2008-06-12 2009-06-12 156 Quality Controller 2001-0

关于这个问题,我有一个问题。我想获得申请人的最新职位,请参见下面的查询。结果应该是这样的:

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'')

假设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而不是datesql2008或datetime?编辑我看到最后一个问题已在问题中修复。

请将您的问题设置为可读的格式。所见即所得编辑器的存在是有原因的。而且您可能没有提供足够的数据来回答此问题-表定义的必要性不如示例数据和要选择的数据的简单英语解释。我宁愿使用startdate=select maxstartdate…如何从该语句中插入查询,并将LatestJob作为此处插入查询,EarliestStart作为选择帮助,sumDATEDIFFYEAR,Startdate,isnullEnddate,getdate作为年份根据帮助从EmploymentDetails组中选择不同的u.FirstName+“”+u.LastName作为名称,lj.Position作为LatestPosition,年资,ad.ExpectedSalary,ReplaceSNullAddress1,+','+ISNULLAddress2,+','+ISNULLCity,',',',,'作为用户地址u加入最新作业lj ON u.Id=lj.aid加入最早开始ye ON ye.aid=u.Id加入应用程序详细信息ad.aid=u.IdI更新了我的答案,但我猜你当时一定已经找到答案了!