我有一份当前职位/职位的员工名单,其开始日期为职位和工作开始日期。
我需要一个查询来生成当前位置作为一个结果,如果该位置的开始日期与就业开始日期不匹配,则显示作为第二结果的历史位置。
数据
[Employee Number] Position [Start Date] [Employment Start Date]
-----------------------------------------------------------------------
12345 Admin 01/01/2017 01/01/2016
输出
[Employee Number] Position [Start Date] [End Date]
-----------------------------------------------------------
12345 Admin 01/01/2017 NULL
12345 Historic 01/01/2016 31/12/2017
答案 0 :(得分:0)
您可以使用union all
:
select employee_number, position, start_date, cast(null as date) end_date
from data
union all
select employee_number, position, employment_start_date, dateadd(day, -1, start_date)
from data
where employment_start_date < start_date;
答案 1 :(得分:0)
SELECT
[Employee Number],
[Position],
[Start Date],
NULL AS [End Date]
FROM
TABLE_NAME
UNION ALL
SELECT
[Employee Number],
'Historic' AS [Position],
[Employment Start Date] AS [Start Date],
DATEADD( DAY,-1,[Start Date] ) AS [End Date]
FROM
TABLE_NAME
WHERE
[Start Date] <> [Employment Start Date]
答案 2 :(得分:0)
WITH table1 AS (
SELECT * FROM (
VALUES
('12345', 'Admin','2017-01-01','2016-01-01')
) AS a (EmployeeNumber, position,StartDate,EmploymentStartDate)
)
select EmployeeNumber, position,StartDate, CASE WHEN StartDate!=EmploymentStartDate THEN NULL ELSE EmploymentStartDate END AS EndDate from table1
union
Select EmployeeNumber,'Historic',EmploymentStartDate as StartDate,DATEADD(dd,-1,DATEADD(yy,1,StartDate)) AS EndDate
From table1 where StartDate!=EmploymentStartDate