生成存储在表中的两个日期之间的所有日期

时间:2016-04-16 19:53:27

标签: sql sql-server date

SQL Server 2014

我需要从表#data生成开始日期和结束日期之间的所有日期。表#data包含几行,包含开始日期和结束日期。

CREATE TABLE #data  (
    StartDate DATE NOT NULL,
    EndDate DATE NOT NULL,
    Opt INT NOT NULL)

insert into #data values('2016-04-10', '2016-04-12', 2)
insert into #data values('2016-04-10', '2016-04-15', 3)
insert into #data values('2016-05-10', '2016-05-12', 4)

表#data可能包含数百行。

我的最终选择应包含:

2016-04-10 2
2016-04-11 2
2016-04-12 2
2016-04-10 3
2016-04-11 3
2016-04-12 3
2016-04-13 3
2016-04-14 3
2016-04-15 3
2016-05-10 4
2016-05-11 4
2016-05-12 4

我目前有这个,但我必须扩展#data中所有行的选择:

DECLARE @StartDateTime DATETIME
DECLARE @EndDateTime DATETIME

SET @StartDateTime = '2015-01-01'
SET @EndDateTime = '2015-01-12';

--delete from #tmp

;WITH DateRange(DateData) AS 
(
    SELECT @StartDateTime as Date
    UNION ALL
    SELECT DATEADD(d,1,DateData)
    FROM DateRange 
    WHERE DateData < @EndDateTime
)
INSERT INTO #tmp SELECT DateData, 1 -- instead of 1 shall be Opt
FROM DateRange
OPTION (MAXRECURSION 0)

select * from #tmp

谢谢。

2 个答案:

答案 0 :(得分:5)

也许这就是你想要的?

;WITH DateRange(DateData, EndDate, Opt) AS 
(
    SELECT StartDate, EndDate , Opt FROM #data
    UNION ALL
    SELECT DATEADD(d,1,DateData), EndDate, Opt
    FROM DateRange 
    WHERE DateData < EndDate
)
SELECT DateData, Opt
FROM DateRange
ORDER BY opt, DateData
OPTION (MAXRECURSION 0)

我会考虑生成一个合适的日历表。如果你有一个,你可以更有效地完成结果(通过使用日历表的连接)。

有关示例和更多信息,请参阅这些文章:

http://www.sqlservercentral.com/blogs/dwainsql/2014/03/30/calendar-tables-in-t-sql/

https://www.mssqltips.com/sqlservertip/4054/creating-a-date-dimension-or-calendar-table-in-sql-server/

答案 1 :(得分:1)

试试这个

;WITH DateRange
AS 
(
    SELECT Startdate, Enddate, Opt from #data 
    UNION ALL
    SELECT DATEADD(d,1,Startdate), Enddate, Opt
    FROM DateRange 
    WHERE Startdate < Enddate
)
SELECT Startdate, Opt
FROM DateRange
Order by Opt

<强>结果

Startdate   Opt
---------------
2016-04-10  2
2016-04-11  2
2016-04-12  2
2016-04-10  3
2016-04-11  3
2016-04-12  3
2016-04-13  3
2016-04-14  3
2016-04-15  3
2016-05-10  4
2016-05-11  4
2016-05-12  4