我有一张桌子:'项目'
CREATE TABLE [dbo].[Project](
[Task_ID] [int] NULL,
[Start_Date] [date] NULL,
[End_Date] [date] NULL
) ON [PRIMARY]
GO
此表中的数据是 -
1 2015-10-01 2015-10-02
2 2015-10-02 2015-10-03
3 2015-10-03 2015-10-04
4 2015-10-13 2015-10-14
5 2015-10-14 2015-10-15
6 2015-10-28 2015-10-29
7 2015-10-30 2015-10-31
Start_Date和End_Date之间的差异保证1天。 我需要找到这些数据序列之间缺失的空白。
期望的输出:
2015-10-05 2015-10-12
2015-10-16 2015-10-27
2015-10-29 2015-10-30
到目前为止我能够得到这么多 -
SELECT [Start_Date]
,MIN([End_Date]) As [End_Date]
,DATEDIFF(DAY, [Start_Date], MIN([End_Date])) As Duration
FROM (
SELECT [Start_Date]
FROM Project
WHERE [Start_Date] NOT IN (
SELECT [End_Date]
FROM Project
)
) A
,(
SELECT [End_Date]
FROM Project
WHERE [End_Date] NOT IN (
SELECT [Start_Date]
FROM Project
)
) B
WHERE [Start_Date] < [End_Date]
GROUP BY [Start_Date]
给了我这个 -
Start_Date End_Date Duration
2015-10-01 2015-10-04 3
2015-10-13 2015-10-15 2
2015-10-28 2015-10-29 1
2015-10-30 2015-10-31 1
有任何建议如何从这里继续进行。我真的被困在这里了。提前谢谢。