StartDate和EndDate之间按季度计的天数-SQL Server

时间:2018-12-18 12:46:40

标签: sql-server date select time period

我需要按季度的天数填充列。期限约为10年。表的结构如下。谢谢你的建议

SELECT StartDate, EndDate
INTO #tmp_RTX 
FROM DateTable


         StartDate                 EndDate
-------------------------------------------------
2015-11-01 00:00:00.000  2018-06-01 00:00:00.000
2017-09-02 00:00:00.000  2021-12-02 00:00:00.000
2016-01-02 00:00:00.000  2019-01-02 00:00:00.000
      .
      .
      .

2018-10-26 00:00:00.000  2020-10-26 00:00:00.000

  INSERT INTO DWHMart.[RTX].[RoadTax]
      (  ,[NuberOfaDaysInQuarter1]
         ,[NuberOfaDaysInQuarter2]
         ,[NuberOfaDaysInQuarter3]
         ,[NuberOfaDaysInOctoberNovember]
         ,[NuberOfaDaysInDecember]
         ,[NuberOfaDaysInTotal])

SELECT 
,[NuberOfaDaysInQuarter1] = NuberOfaDaysInQuarter1
,[NuberOfaDaysInQuarter2] = NuberOfaDaysInQuarter2
,[NuberOfaDaysInQuarter3] = NuberOfaDaysInQuarter3
,[NuberOfaDaysInOctoberNovember] = NuberOfaDaysInOctoberNovember
,[NuberOfaDaysInDecember] = NuberOfaDaysInDecember
,[NuberOfaDaysInTotal] = DATEDIFF(DAY, StartDate, EndDate)
FROM #tmp_RTX 

预期结果:

StartDate                 EndDate                 NuberOfaDaysInQuarter1   ..
2015-11-01 00:00:00.000  2018-06-01 00:00:00.000        .
2017-09-02 00:00:00.000  2021-12-02 00:00:00.000        .
2016-01-02 00:00:00.000  2019-01-02 00:00:00.000        .
      .                                                 .
      .                                                 .
      .                                                 .

2018-10-26 00:00:00.000  2020-10-26 00:00:00.000        .

3 个答案:

答案 0 :(得分:0)

除the年外,每季度的天数始终相同。因此,可以将常量表达式用于Q2..Q4,并将参数化的值用于Q1。

DECLARE @my_date date = '2017-01-01';
SELECT 89 + ISDATE(CAST(@my_date AS char(4)) + '0229') AS NuberOfaDaysInQuarter1,
       90 AS NuberOfaDaysInQuarter2,
       91 AS NuberOfaDaysInQuarter3,
       91 AS NuberOfaDaysInQuarter4

更新的解决方案(按季度累计天数)

DECLARE @start_date date = '2000-01-01';
DECLARE @quarters TABLE (start_date date, end_date date, num int);
-- Create quarter calendar from @start_date
WITH nums (n) AS
(
   SELECT TOP 100 (ROW_NUMBER() OVER (ORDER BY (SELECT 1)) - 1) * 3
   FROM sys.columns sc1 CROSS JOIN sys.columns sc2
),
calendar AS
(
   SELECT n, DATEADD(month, n, @start_date) AS start_date
   FROM nums
)
INSERT INTO @quarters
SELECT start_date, 
       DATEADD(day, -1, DATEADD(month, 3, start_date)) AS end_date,
       ((n / 3) % 4) + 1 AS num
FROM calendar;

-- test data
DECLARE @t TABLE (start_date date, end_date date);
INSERT INTO @t VALUES
('2015-11-01', '2018-06-01'),
('2017-09-02', '2021-12-02'),
('2016-01-02', '2019-01-02');

-- Calculations
WITH inner_counts AS (
   SELECT t.start_date, t.end_date, q.num,
          SUM(datediff(day, q.start_date, q.end_date)) AS days_count
   FROM @t t INNER JOIN @quarters q ON q.start_date > t.start_date AND q.start_date < t.end_date
   GROUP BY t.start_date, t.end_date, q.num
),
outer_counts1 AS (
   SELECT t.start_date, t.end_date, q.num,
          SUM(datediff(day, t.start_date, q.end_date)) AS days_count
   FROM @t t INNER JOIN @quarters q ON q.end_date = (SELECT MIN(end_date) FROM @quarters q WHERE q.end_date >= t.start_date)
   GROUP BY t.start_date, t.end_date, q.num
),
outer_counts2 AS (
   SELECT t.start_date, t.end_date, q.num,
          SUM(datediff(day, q.start_date, t.end_date)) AS days_count
   FROM @t t INNER JOIN @quarters q ON q.start_date = (SELECT MAX(start_date) FROM @quarters q WHERE q.start_date <= t.end_date)
   GROUP BY t.start_date, t.end_date, q.num
),
total_counts AS (
   SELECT start_date, end_date, num, SUM(days_count) AS days_count
   FROM (SELECT * FROM inner_counts 
         UNION ALL 
         SELECT * FROM outer_counts1 
         UNION ALL 
         SELECT * FROM outer_counts2) c
   GROUP BY start_date, end_date, num
)
SELECT start_date, end_date,
       SUM(CASE WHEN num = 1 THEN days_count ELSE 0 END) AS days_in_Q1,
       SUM(CASE WHEN num = 2 THEN days_count ELSE 0 END) AS days_in_Q2,
       SUM(CASE WHEN num = 3 THEN days_count ELSE 0 END) AS days_in_Q3,
       SUM(CASE WHEN num = 4 THEN days_count ELSE 0 END) AS days_in_Q4
FROM total_counts
GROUP BY start_date, end_date

结果

start_date end_date   days_in_Q1  days_in_Q2  days_in_Q3  days_in_Q4
---------- ---------- ----------- ----------- ----------- -----------
2015-11-01 2018-06-01 268         331         182         242
2016-01-02 2019-01-02 357         270         273         273
2017-09-02 2021-12-02 357         360         392         517

答案 1 :(得分:0)

我已经使用递归CTE制作了自己的动态天数表。

然后我按四分之一数计算出了您给定的每个范围内的天数

我对您的每一行都使用了CROSS JOIN,因为它们对于GROUP BY没有唯一的ID,但是在任何情况下都可以使用

;WITH RNG AS (SELECT cast(MIN(StartDate) as date) as MN, 
                 cast(MAX (EndDate ) as date) MX FROM #tmp_RTX),
 DATS AS (SELECT MN FROM RNG
          UNION ALL 
          SELECT DATEADD(day,1,MN) FROM dats 
            WHERE dats.MN < (SELECT MX FROM RNG)
        )
        SELECT StartDate T1,
                Enddate T1, 
                DQ.NuberOfaDaysInQuarter1   ,
                DQ.NuberOfaDaysInQuarter2   ,
                DQ.NuberOfaDaysInQuarter3  , 
                DQ.NuberOfaDaysInQuarter4   
                    FROM #tmp_RTX T1
                        CROSS APPLY (SELECT 
                                    SUM(CASE WHEN datepart(quarter ,MN) = 1 THEN 1 ELSE 0 END) 
                                            NuberOfaDaysInQuarter1,
                                    SUM(CASE WHEN datepart(quarter ,MN) = 2 THEN 1 ELSE 0 END) 
                                            NuberOfaDaysInQuarter2,      
                                    SUM(CASE WHEN datepart(quarter ,MN) = 3 THEN 1 ELSE 0 END) 
                                            NuberOfaDaysInQuarter3,
                                    SUM(CASE WHEN datepart(quarter ,MN) = 4 THEN 1 ELSE 0 END) 
                                            NuberOfaDaysInQuarter4
                                                 FROM DATS
                                                 WHERE MN BETWEEN 
                                                            CAST(t1.startdate as date) 
                                                                and 
                                                                CAST(T1.EndDate as date)
                            ) DQ

        OPTION  (maxrecursion 0);

以下是构建我的测试数据的依据(基于您的数据约定)

SELECT '20151101' StartDate, '20180601' EndDate
INTO #tmp_RTX 

INSERT into #tmp_RTX  values ('20161231','20180101');

INSERT into #tmp_RTX  values ('20181231','20190101');

要处理最后一个季度的不同要求,请检查月份,而不是季度

;WITH RNG AS (SELECT cast(MIN(StartDate) as date) as MN, 
                 cast(MAX (EndDate ) as date) MX FROM #tmp_RTX),
 DATS AS (SELECT MN FROM RNG
          UNION ALL 
          SELECT DATEADD(day,1,MN) FROM dats 
            WHERE dats.MN < (SELECT MX FROM RNG)
        )
        SELECT StartDate T1,
                Enddate T1, 
                DQ.NuberOfaDaysInQuarter1   ,
                DQ.NuberOfaDaysInQuarter2   ,
                DQ.NuberOfaDaysInQuarter3  , 
                DQ.NuberOfaDaysInOctoberNovember   ,
                DQ.NuberOfaDaysInDecember
                    FROM #tmp_RTX T1
                        CROSS APPLY (SELECT 
                                    SUM(CASE WHEN datepart(quarter ,MN) = 1 THEN 1 ELSE 0 END) 
                                            NuberOfaDaysInQuarter1,
                                    SUM(CASE WHEN datepart(quarter ,MN) = 2 THEN 1 ELSE 0 END) 
                                            NuberOfaDaysInQuarter2,      
                                    SUM(CASE WHEN datepart(quarter ,MN) = 3 THEN 1 ELSE 0 END) 
                                            NuberOfaDaysInQuarter3,
                                    SUM(CASE WHEN datepart(month ,MN) IN(10,11) THEN 1 ELSE 0 END) 
                                            NuberOfaDaysInOctoberNovember,
                                    SUM(CASE WHEN datepart(month ,MN) IN(12) THEN 1 ELSE 0 END) 
                                            NuberOfaDaysInDecember
                                                 FROM DATS
                                                 WHERE MN BETWEEN 
                                                            CAST(t1.startdate as date) 
                                                                and 
                                                                CAST(T1.EndDate as date)
                            ) DQ

        OPTION  (maxrecursion 0)

答案 2 :(得分:0)

DEMO

使用递归CTE(请确保将max recursion选项设置为需要循环处理的天数,或者在确定不存在无限循环后将其设置为0。)来生成介于max和min之间的所有天数在日期表中添加日期,然后将此结果与您的日期表(范围之间的日期)合并。在周期日期的年和季度上使用datepart,我们可以获得适当的季度/年,然后进行分组。

我并没有考虑结果。透视数据通常是一种显示功能,最好留给那些显示工具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 |
+----+---------------------+---------------------+--------+---------------+------------+---------+-----------+