DEMO
使用递归 CTE(请务必将最大递归选项设置为您需要循环处理的天数,或者在您确认不存在无限循环后设置为 0。)生成您的最大和最小天数之间的所有天数日期表中的日期,然后将此结果加入到日期范围之间的日期表中。在周期日期的年份和季度上使用日期部分,我们可以获得适当的季度/年度,然后进行分组。
我没有调整结果。透视数据通常是一种显示功能,最好留给那些显示工具 Crystal、BI 等。它可以在 SQL 中完成,在这种情况下,将需要动态 SQL,因为年数本质上是动态的。我选择不走这条路,因为报告工具可以处理数据的动态特性。
WITH
DateTable as (SELECT cast('2015-11-01 00:00:00.000'as date) startdate, cast('2018-06-01 00:00:00.000'as date) endDate union all
SELECT '2017-09-02 00:00:00.000', '2021-12-02 00:00:00.000' union all
SELECT '2016-01-02 00:00:00.000', '2019-01-02 00:00:00.000'),
CTE AS (SELECT Min(StartDate) StartDate, max(EndDate) EndDate FROM DATETABLE),
CTE2 AS (SELECT C.StartDate as RangeStartDate
, C.EndDate as RangeEndDate
, 1 CycleCount
, datepart(Q,dateadd(d,0,C.StartDate)) as Quarter
, datepart(m,dateadd(d,0,C.StartDate)) as Month
, datepart(YYYY, dateadd(d,0,C.StartDate)) as Yr
, C.StartDate as CycleDate
FROM CTE C
UNION ALL
SELECT RangeStartDate, RangeEndDate, CycleCount+1
, datepart(Q,dateadd(d,1,CycleDate)) as Quarter
, datepart(m,dateadd(d,1,CycleDate)) as Month
, datepart(YYYY, dateadd(d,1,CycleDate)) as Yr
, dateadd(d,1,cycleDate) as CycleDate
FROM cte2
WHERE datediff(d,RangeStartDate, RangeEndDate) >= CycleCount
)
SELECT DT.StartDate
, DT.ENDDate
, concat(yr,'-',Quarter) [YYYY-Q]
, count(*) as DaysInQuarter
, sum(case when Month in (10,11) then 1 else 0 end)as OctNovDays
, sum(case when Month in (12) then 1 else 0 end)as DecDays
, datediff(d,StartDate, EndDate) as DaysTotal
FROM CTE2
INNER JOIN DateTable DT
on CTE2.CycleDate between DT.StartDate and DT.EndDate
GROUP BY Quarter,DT.StartDate, DT.EndDate, YR
ORDER BY DT.StartDate, DT.EndDate, YR, Quarter
OPTION (MAXRECURSION 10000)
给我们:
+----+---------------------+---------------------+--------+---------------+------------+---------+-----------+
| | StartDate | ENDDate | YYYY-Q | DaysInQuarter | OctNovDays | DecDays | DaysTotal |
+----+---------------------+---------------------+--------+---------------+------------+---------+-----------+
| 1 | 01.11.2015 00:00:00 | 01.06.2018 00:00:00 | 2015-4 | 61 | 30 | 31 | 943 |
| 2 | 01.11.2015 00:00:00 | 01.06.2018 00:00:00 | 2016-1 | 91 | 0 | 0 | 943 |
| 3 | 01.11.2015 00:00:00 | 01.06.2018 00:00:00 | 2016-2 | 91 | 0 | 0 | 943 |
| 4 | 01.11.2015 00:00:00 | 01.06.2018 00:00:00 | 2016-3 | 92 | 0 | 0 | 943 |
| 5 | 01.11.2015 00:00:00 | 01.06.2018 00:00:00 | 2016-4 | 92 | 61 | 31 | 943 |
| 6 | 01.11.2015 00:00:00 | 01.06.2018 00:00:00 | 2017-1 | 90 | 0 | 0 | 943 |
| 7 | 01.11.2015 00:00:00 | 01.06.2018 00:00:00 | 2017-2 | 91 | 0 | 0 | 943 |
| 8 | 01.11.2015 00:00:00 | 01.06.2018 00:00:00 | 2017-3 | 92 | 0 | 0 | 943 |
| 9 | 01.11.2015 00:00:00 | 01.06.2018 00:00:00 | 2017-4 | 92 | 61 | 31 | 943 |
| 10 | 01.11.2015 00:00:00 | 01.06.2018 00:00:00 | 2018-1 | 90 | 0 | 0 | 943 |
| 11 | 01.11.2015 00:00:00 | 01.06.2018 00:00:00 | 2018-2 | 62 | 0 | 0 | 943 |
| 12 | 02.01.2016 00:00:00 | 02.01.2019 00:00:00 | 2016-1 | 90 | 0 | 0 | 1096 |
| 13 | 02.01.2016 00:00:00 | 02.01.2019 00:00:00 | 2016-2 | 91 | 0 | 0 | 1096 |
| 14 | 02.01.2016 00:00:00 | 02.01.2019 00:00:00 | 2016-3 | 92 | 0 | 0 | 1096 |
| 15 | 02.01.2016 00:00:00 | 02.01.2019 00:00:00 | 2016-4 | 92 | 61 | 31 | 1096 |
| 16 | 02.01.2016 00:00:00 | 02.01.2019 00:00:00 | 2017-1 | 90 | 0 | 0 | 1096 |
| 17 | 02.01.2016 00:00:00 | 02.01.2019 00:00:00 | 2017-2 | 91 | 0 | 0 | 1096 |
| 18 | 02.01.2016 00:00:00 | 02.01.2019 00:00:00 | 2017-3 | 92 | 0 | 0 | 1096 |
| 19 | 02.01.2016 00:00:00 | 02.01.2019 00:00:00 | 2017-4 | 92 | 61 | 31 | 1096 |
| 20 | 02.01.2016 00:00:00 | 02.01.2019 00:00:00 | 2018-1 | 90 | 0 | 0 | 1096 |
| 21 | 02.01.2016 00:00:00 | 02.01.2019 00:00:00 | 2018-2 | 91 | 0 | 0 | 1096 |
| 22 | 02.01.2016 00:00:00 | 02.01.2019 00:00:00 | 2018-3 | 92 | 0 | 0 | 1096 |
| 23 | 02.01.2016 00:00:00 | 02.01.2019 00:00:00 | 2018-4 | 92 | 61 | 31 | 1096 |
| 24 | 02.01.2016 00:00:00 | 02.01.2019 00:00:00 | 2019-1 | 2 | 0 | 0 | 1096 |
| 25 | 02.09.2017 00:00:00 | 02.12.2021 00:00:00 | 2017-3 | 29 | 0 | 0 | 1552 |
| 26 | 02.09.2017 00:00:00 | 02.12.2021 00:00:00 | 2017-4 | 92 | 61 | 31 | 1552 |
| 27 | 02.09.2017 00:00:00 | 02.12.2021 00:00:00 | 2018-1 | 90 | 0 | 0 | 1552 |
| 28 | 02.09.2017 00:00:00 | 02.12.2021 00:00:00 | 2018-2 | 91 | 0 | 0 | 1552 |
| 29 | 02.09.2017 00:00:00 | 02.12.2021 00:00:00 | 2018-3 | 92 | 0 | 0 | 1552 |
| 30 | 02.09.2017 00:00:00 | 02.12.2021 00:00:00 | 2018-4 | 92 | 61 | 31 | 1552 |
| 31 | 02.09.2017 00:00:00 | 02.12.2021 00:00:00 | 2019-1 | 90 | 0 | 0 | 1552 |
| 32 | 02.09.2017 00:00:00 | 02.12.2021 00:00:00 | 2019-2 | 91 | 0 | 0 | 1552 |
| 33 | 02.09.2017 00:00:00 | 02.12.2021 00:00:00 | 2019-3 | 92 | 0 | 0 | 1552 |
| 34 | 02.09.2017 00:00:00 | 02.12.2021 00:00:00 | 2019-4 | 92 | 61 | 31 | 1552 |
| 35 | 02.09.2017 00:00:00 | 02.12.2021 00:00:00 | 2020-1 | 91 | 0 | 0 | 1552 |
| 36 | 02.09.2017 00:00:00 | 02.12.2021 00:00:00 | 2020-2 | 91 | 0 | 0 | 1552 |
| 37 | 02.09.2017 00:00:00 | 02.12.2021 00:00:00 | 2020-3 | 92 | 0 | 0 | 1552 |
| 38 | 02.09.2017 00:00:00 | 02.12.2021 00:00:00 | 2020-4 | 92 | 61 | 31 | 1552 |
| 39 | 02.09.2017 00:00:00 | 02.12.2021 00:00:00 | 2021-1 | 90 | 0 | 0 | 1552 |
| 40 | 02.09.2017 00:00:00 | 02.12.2021 00:00:00 | 2021-2 | 91 | 0 | 0 | 1552 |
| 41 | 02.09.2017 00:00:00 | 02.12.2021 00:00:00 | 2021-3 | 92 | 0 | 0 | 1552 |
| 42 | 02.09.2017 00:00:00 | 02.12.2021 00:00:00 | 2021-4 | 63 | 61 | 2 | 1552 |
+----+---------------------+---------------------+--------+---------------+------------+---------+-----------+