使用 SQL 将日期范围拆分为多行

Split date range to multiple rows using SQL

我有一个table:

startdate                 enddate                   other_columns
1956-05-06 00:00:00.000   1960-04-05 00:00:00.000   myvalues

我需要一个查询 return 结果为:

startdate                 enddate                   other_columns
1956-05-06 00:00:00.000   1956-12-31 00:00:00.000   myvalues
1957-01-01 00:00:00.000   1957-12-31 00:00:00.000   myvalues
1958-01-01 00:00:00.000   1958-12-31 00:00:00.000   myvalues
1959-01-01 00:00:00.000   1959-12-31 00:00:00.000   myvalues
1960-01-01 00:00:00.000   1960-04-05 00:00:00.000   myvalues

基本上是一个将行分解为年度结果的查询。我需要保留开始和结束日期。

CREATE TABLE #InputTABLE
(
startdate DATETIME,
enddate DATETIME,
other_columns varchar(20) 
)

INSERT INTO #InputTABLE VALUES('1956-05-06','1960-04-05','myvalues');

SELECT * FROM #InputTABLE

输出:

    startdate                 enddate                   other_columns
    1956-05-06 00:00:00.000   1960-04-05 00:00:00.000   myvalues

查询:

CREATE TABLE #OutputTABLE
(
startdate DATETIME,
enddate DATETIME,
other_columns varchar(20) 
)

DECLARE @cnt int
DECLARE @startDate datetime
DECLARE @endDate datetime
DECLARE @incr int
DECLARE @tempDate datetime 

SET @startDate=(Select startdate from #InputTABLE)
SET @endDate=(Select enddate from #InputTABLE)
SET @cnt=DATEDIFF(yy,@startDate,@endDate)
SET @incr=0

SET @tempDate=DATEADD(yy,@incr,Cast(@startDate As datetime))

WHILE @cnt>=0
BEGIN

   IF @cnt = 0 
      BEGIN
         INSERT INTO #OutputTABLE VALUES(@tempDate,@endDate,'myvalues');
      END
   ELSE
      BEGIN
         insert into #OutputTABLE values(@tempDate,DATEADD(yy, DATEDIFF(yy,0,@tempDate)+1, -1),'myvalues');
      END
   SET @tempDate=DATEADD(yy,@incr+1,DATEADD(yy,DATEDIFF(yy,0,@startDate),0))

   SET @cnt=@cnt-1
   SET @incr=@incr+1

END

结果:SELECT * FROM #OutputTABLE;

startdate                 enddate                   other_columns
1956-05-06 00:00:00.000   1956-12-31 00:00:00.000   myvalues
1957-01-01 00:00:00.000   1957-12-31 00:00:00.000   myvalues
1958-01-01 00:00:00.000   1958-12-31 00:00:00.000   myvalues
1959-01-01 00:00:00.000   1959-12-31 00:00:00.000   myvalues
1960-01-01 00:00:00.000   1960-04-05 00:00:00.000   myvalues

查询很简单

SELECT CASE WHEN yrStart<it.startdate THEN it.startdate ELSE yrStart END AS startdate,
       CASE WHEN yrEnd>it.enddate THEN it.enddate ELSE yrEnd END AS enddate,
       other_columns
FROM #InputTABLE it
CROSS APPLY
    (SELECT datefromparts(yr, 1, 1) yrStart, datefromparts(yr, 12, 31) yrEnd
     FROM dbo.yearTable 
     WHERE yr >= datepart(year, it.startdate) AND yr <= datepart(year, it.enddate)
    )years;

您只需要一个带有数字 1..9999 的年表(又名 Tally table)。在这个 table 中,你不应该有超出这个范围的数字,否则你会遇到一些令人讨厌的转换错误。