DECLARE @T1 TABLE ([ID] INT, [WD] DATE, [SD] DATETIME, [ED] DATETIME, [Val] INT)
INSERT INTO @T1 ([ID], [WD], [SD], [ED], [VAL])
VALUES (1, '2016-02-14', '2016-02-14 08:00:00', '2016-02-14 16:30:00', 12)
,(1, '2016-02-14', '2016-02-16 08:00:00', '2016-02-16 16:30:00', 15)
,(3, '2016-02-14', '2016-02-16 08:00:00', '2016-02-16 16:30:00', 12)
,(1, '2016-02-21', '2016-02-22 08:00:00', '2016-02-22 16:30:00', 100)
,(2, '2016-02-21', '2016-02-25 08:00:00', '2016-02-25 16:30:00', 124)
,(3, '2016-03-20', '2016-03-21 08:00:00', '2016-03-21 16:30:00', 10)
,(3, '2016-04-17', '2016-04-17 08:00:00', '2016-04-17 16:30:00', 8);
DECLARE @StartDate DATE
, @EndDate DATE;
SELECT @StartDate = MIN(WD) FROM @T1
SELECT @EndDate = DATEADD(DAY,6,MAX(WD)) FROM @T1;
DECLARE @T2 TABLE (D DATE) -- Table generated to get ALL dates between the minium WD (Min Sunday) and 6 days after the maximum WD (Max Sunday + 6 days to complete the week date range)
INSERT INTO @T2 ([D])
SELECT DATEADD(DAY, NBR - 1, @StartDate)
FROM (SELECT ROW_NUMBER() OVER (ORDER BY c.object_id) AS NBR
FROM sys.columns c
) NBR
WHERE NBR - 1 <= DATEDIFF(DAY, @StartDate, @EndDate);
SELECT * FROM @T1;
SELECT * FROM @T2;
描述: @ T1是與特定日期的預填充源數據表,其中[ID] =某種類型的ID字段,[ WD] =每週的開始日期(星期日),[SD] =該[WD]星期內的一天開始,[ED] =該[WD]星期內的一天結束,[Val] = ([WD])和MAX([WD])+ 6天以內的所有日期定義和填充的表格(強制使用該表格)周)
目標:
返回@ T1數據以及這些[ID]/[WD]星期的任何缺失日期。 (參見所需的輸出爲更好地理解)
期望的結果(從Excel中複製所以格式有所不同,但沒關係)
[ID][WD] [SD] [ED] [Val]
1 2/14/2016 2/14/16 8:00 2/14/16 16:30 12
1 2/14/2016 2/15/16 00:00 2/15/16 00:00 NULL
1 2/14/2016 2/16/16 8:00 2/16/16 16:30 15
1 2/14/2016 2/17/16 00:00 2/17/16 00:00 NULL
1 2/14/2016 2/18/16 00:00 2/18/16 00:00 NULL
1 2/14/2016 2/19/16 00:00 2/19/16 00:00 NULL
1 2/14/2016 2/20/16 00:00 2/20/16 00:00 NULL
3 2/14/2016 2/14/16 00:00 2/14/16 00:00 NULL
3 2/14/2016 2/15/16 00:00 2/15/16 00:00 NULL
3 2/14/2016 2/16/16 8:00 2/16/16 16:30 12
3 2/14/2016 2/17/16 00:00 2/17/16 00:00 NULL
3 2/14/2016 2/18/16 00:00 2/18/16 00:00 NULL
3 2/14/2016 2/19/16 00:00 2/19/16 00:00 NULL
3 2/14/2016 2/20/16 00:00 2/20/16 00:00 NULL
1 2/21/2016 2/21/16 00:00 2/21/16 00:00 NULL
1 2/21/2016 2/22/16 8:00 2/22/16 16:30 100
1 2/21/2016 2/23/16 00:00 2/23/16 00:00 NULL
1 2/21/2016 2/24/16 00:00 2/24/16 00:00 NULL
1 2/21/2016 2/25/16 00:00 2/25/16 00:00 NULL
1 2/21/2016 2/26/16 00:00 2/26/16 00:00 NULL
1 2/21/2016 2/27/16 00:00 2/27/16 00:00 NULL
2 2/21/2016 2/21/16 00:00 2/21/16 00:00 NULL
2 2/21/2016 2/22/16 00:00 2/22/16 00:00 NULL
2 2/21/2016 2/23/16 00:00 2/23/16 00:00 NULL
2 2/21/2016 2/24/16 00:00 2/24/16 00:00 NULL
2 2/21/2016 2/25/16 8:00 2/25/16 16:30 124
2 2/21/2016 2/26/16 00:00 2/26/16 00:00 NULL
2 2/21/2016 2/27/16 00:00 2/27/16 00:00 NULL
3 3/20/2016 3/20/16 00:00 3/20/16 00:00 NULL
3 3/20/2016 3/21/16 8:00 3/21/16 16:30 10
3 3/20/2016 3/22/16 00:00 3/22/16 00:00 NULL
3 3/20/2016 3/23/16 00:00 3/23/16 00:00 NULL
3 3/20/2016 3/24/16 00:00 3/24/16 00:00 NULL
3 3/20/2016 3/25/16 00:00 3/25/16 00:00 NULL
3 3/20/2016 3/26/16 00:00 3/26/16 00:00 NULL
3 4/17/2016 4/17/16 8:00 4/17/16 16:30 8
3 4/17/2016 4/18/16 00:00 4/18/16 00:00 NULL
3 4/17/2016 4/19/16 00:00 4/19/16 00:00 NULL
3 4/17/2016 4/20/16 00:00 4/20/16 00:00 NULL
3 4/17/2016 4/21/16 00:00 4/21/16 00:00 NULL
3 4/17/2016 4/22/16 00:00 4/22/16 00:00 NULL
3 4/17/2016 4/23/16 00:00 4/23/16 00:00 NULL
我使用交叉連接之類的東西,但試了沒有運氣得到所需輸出。
謝謝你的創意sln。它返回預期的數據。 :) – 007