使用四个与同一位员工的时间轴重叠的数据源,我需要创建一个主时间轴,该主时间轴为员工提供准确,不间断的时间轴,通过优先级使用源数据。
A> B> C> D
如果时间轴中的日期被A覆盖,则使用其他B等。
如果日期同时在A和B中都使用A。
如果日期不在A中而是在B中,请使用B等。
我尝试仅在选择适当来源的情况下将它们组合在一起,但是这要求将时间表划分为单个日期,该日期适用于一名员工,但是覆盖10.000名员工非常缓慢。
/* Drop tables */
DROP TABLE IF EXISTS #A
DROP TABLE IF EXISTS #B
DROP TABLE IF EXISTS #C
DROP TABLE IF EXISTS #D
DROP TABLE IF EXISTS #Output
/* Create temporay tables */
CREATE TABLE #A ([TableKey] varbinary(8000), [StartDate] date, [EndDate] date, [Source] varchar(2))
CREATE TABLE #B ([TableKey] varbinary(8000), [StartDate] date, [EndDate] date, [Source] varchar(2))
CREATE TABLE #C ([TableKey] varbinary(8000), [StartDate] date, [EndDate] date, [Source] varchar(2))
CREATE TABLE #D ([TableKey] varbinary(8000), [StartDate] date, [EndDate] date, [Source] varchar(2))
CREATE TABLE #Output ([TableKey] varbinary(8000), [Team] VARCHAR(6), [StartDate] date, [EndDate] DATE, [Source] varchar(2))
/* Insert data */
INSERT INTO #A
VALUES
( 0x20e5cc5f1e06b501c65b079efe68ca60, N'2013-09-10', N'2014-08-11', 'A' ),
( 0x20e5cc5f1e06b501c65b079efe68ca60, N'2014-08-11', N'2014-09-29', 'A' ),
( 0x20e5cc5f1e06b501c65b079efe68ca60, N'2014-09-29', N'2017-08-24', 'A' ),
( 0x20e5cc5f1e06b501c65b079efe68ca60, N'2017-08-24', N'2018-10-01', 'A' ),
( 0x20e5cc5f1e06b501c65b079efe68ca60, N'2018-10-01', N'2019-03-04', 'A' ),
( 0x20e5cc5f1e06b501c65b079efe68ca60, N'2019-03-04', N'2037-01-01', 'A' ),
( 0x7ccb1e3f22333f646f6a2f8e90cdc23f, N'2013-09-10', N'2013-11-26', 'A' ),
( 0x7ccb1e3f22333f646f6a2f8e90cdc23f, N'2013-11-26', N'2014-05-29', 'A' ),
( 0x7ccb1e3f22333f646f6a2f8e90cdc23f, N'2014-05-29', N'2014-05-31', 'A' )
INSERT INTO #B
VALUES
( 0x20e5cc5f1e06b501c65b079efe68ca60, N'2015-06-04', N'2015-10-27', 'B' ),
( 0x20e5cc5f1e06b501c65b079efe68ca60, N'2015-10-28', N'2016-05-07', 'B' ),
( 0x20e5cc5f1e06b501c65b079efe68ca60, N'2016-05-08', N'2016-05-17', 'B' ),
( 0x20e5cc5f1e06b501c65b079efe68ca60, N'2016-05-18', N'2016-08-27', 'B' ),
( 0x20e5cc5f1e06b501c65b079efe68ca60, N'2016-08-28', N'2016-11-23', 'B' ),
( 0x20e5cc5f1e06b501c65b079efe68ca60, N'2016-11-24', N'2017-01-14', 'B' ),
( 0x20e5cc5f1e06b501c65b079efe68ca60, N'2017-01-15', N'2017-08-27', 'B' ),
( 0x20e5cc5f1e06b501c65b079efe68ca60, N'2017-08-28', N'2017-09-05', 'B' ),
( 0x20e5cc5f1e06b501c65b079efe68ca60, N'2017-09-06', N'2017-10-07', 'B' ),
( 0x20e5cc5f1e06b501c65b079efe68ca60, N'2017-10-08', N'2017-11-05', 'B' ),
( 0x20e5cc5f1e06b501c65b079efe68ca60, N'2017-11-06', N'2018-09-29', 'B' ),
( 0x20e5cc5f1e06b501c65b079efe68ca60, N'2018-09-30', N'2019-03-12', 'B' ),
( 0x20e5cc5f1e06b501c65b079efe68ca60, N'2019-03-13', N'2019-04-29', 'B' ),
( 0x20e5cc5f1e06b501c65b079efe68ca60, N'2019-04-30', N'2019-05-06', 'B' ),
( 0x20e5cc5f1e06b501c65b079efe68ca60, N'2019-05-07', N'2037-12-29', 'B' ),
( 0x7ccb1e3f22333f646f6a2f8e90cdc23f, N'2015-06-04', N'2015-11-13', 'B' ),
( 0x7ccb1e3f22333f646f6a2f8e90cdc23f, N'2015-11-14', N'2016-11-01', 'B' ),
( 0x7ccb1e3f22333f646f6a2f8e90cdc23f, N'2016-11-02', N'2016-12-17', 'B' ),
( 0x7ccb1e3f22333f646f6a2f8e90cdc23f, N'2016-12-18', N'2017-05-01', 'B' ),
( 0x7ccb1e3f22333f646f6a2f8e90cdc23f, N'2017-05-02', N'2017-08-06', 'B' ),
( 0x7ccb1e3f22333f646f6a2f8e90cdc23f, N'2017-08-07', N'2017-08-28', 'B' ),
( 0x7ccb1e3f22333f646f6a2f8e90cdc23f, N'2017-08-29', N'2017-10-14', 'B' ),
( 0x7ccb1e3f22333f646f6a2f8e90cdc23f, N'2017-10-15', N'2037-12-29', 'B' )
INSERT INTO #C
Values
( 0x65dd7602e1c376a2d991114ab4c5d28d, N'2013-09-09', N'2014-08-10', 'C' ),
( 0x65dd7602e1c376a2d991114ab4c5d28d, N'2014-08-10', N'2014-09-28', 'C' ),
( 0x65dd7602e1c376a2d991114ab4c5d28d, N'2014-09-28', N'2017-08-23', 'C' ),
( 0x65dd7602e1c376a2d991114ab4c5d28d, N'2017-08-23', N'2017-09-07', 'C' ),
( 0x65dd7602e1c376a2d991114ab4c5d28d, N'2017-09-07', N'2018-06-27', 'C' ),
( 0x67d42e1b67016ec584092017703de16f, N'2013-09-09', N'2013-11-25', 'C' ),
( 0x67d42e1b67016ec584092017703de16f, N'2013-11-25', N'2014-05-28', 'C' ),
( 0x67d42e1b67016ec584092017703de16f, N'2014-05-28', N'2014-05-30', 'C' ),
( 0x67d42e1b67016ec584092017703de16f, N'2014-05-30', N'2018-06-27', 'C' )
INSERT INTO #D
VALUES
( 0x20e5cc5f1e06b501c65b079efe68ca60, N'2015-02-14', N'2037-12-29', 'D' ),
( 0x7ccb1e3f22333f646f6a2f8e90cdc23f, N'2015-02-14', N'2037-12-29', 'D' )
两个员工的完整时间轴示例,其中使用代码来源创建时间轴。
/* Desired Output */
INSERT INTO #Output
VALUES
(0x20e5cc5f1e06b501c65b079efe68ca60,'Team 1',N'2013-09-10',N'2014-08-11',N'A'),
(0x20e5cc5f1e06b501c65b079efe68ca60,'Team 2',N'2014-08-11',N'2014-09-29',N'A'),
(0x20e5cc5f1e06b501c65b079efe68ca60,'Team 1',N'2014-09-29',N'2017-08-24',N'A'),
(0x20e5cc5f1e06b501c65b079efe68ca60,'Team 4',N'2017-08-24',N'2018-10-01',N'D'),
(0x20e5cc5f1e06b501c65b079efe68ca60,'Team 1',N'2018-10-01',N'2019-03-04',N'A'),
(0x20e5cc5f1e06b501c65b079efe68ca60,'Team 3',N'2019-03-04',NULL,N'A'),
(0x7CCB1E3F22333F646F6A2F8E90CDC23F,'Team 1',N'2013-09-10',N'2013-11-26','A'),
(0x7CCB1E3F22333F646F6A2F8E90CDC23F,'Team 2',N'2013-11-26',N'2014-05-29','B'),
(0x7CCB1E3F22333F646F6A2F8E90CDC23F,'Team 2',N'2014-05-29',N'2014-05-31','B'),
(0x7CCB1E3F22333F646F6A2F8E90CDC23F,'Team 3',N'2017-02-15',N'2017-03-15','C'),
(0x7CCB1E3F22333F646F6A2F8E90CDC23F,'Team 4',N'2017-03-16',NULL,N'D')