0

问题:在列中显示每个月在两个日期期间之间的周数(现在可以到三个月)。如果可能,从当天开始(动态)

我目前在哪里:

SELECT Q3.[Begin Date]
    ,Q3.[End Date]
    ,Q3.Diff_in_Year
    ,sum(CASE 
            WHEN Q3.Year_Counter = 0
                THEN datediff(mm, Q3.y_start, Q3.y_end) + 1
            ELSE 0
            END) Y1
    ,sum(CASE 
            WHEN Q3.Year_Counter = 1
                THEN datediff(mm, Q3.y_start, Q3.y_end) + 1
            ELSE 0
            END) Y2
    ,sum(CASE 
            WHEN Q3.Year_Counter = 2
                THEN datediff(mm, Q3.y_start, Q3.y_end) + 1
            ELSE 0
            END) Y3
    ,sum(CASE 
            WHEN Q3.Year_Counter = 3
                THEN datediff(mm, Q3.y_start, Q3.y_end) + 1
            ELSE 0
            END) Y4
    ,sum(CASE 
            WHEN Q3.Year_Counter = 4
                THEN datediff(mm, Q3.y_start, Q3.y_end) + 1
            ELSE 0
            END) Y5
    ,sum(CASE 
            WHEN Q3.Year_Counter = 5
                THEN datediff(mm, Q3.y_start, Q3.y_end) + 1
            ELSE 0
            END) Y6
    ,sum(CASE 
            WHEN Q3.Year_Counter = 6
                THEN datediff(mm, Q3.y_start, Q3.y_end) + 1
            ELSE 0
            END) Y7
    ,sum(CASE 
            WHEN Q3.Year_Counter = 7
                THEN datediff(mm, Q3.y_start, Q3.y_end) + 1
            ELSE 0
            END) Y8
    ,sum(CASE 
            WHEN Q3.Year_Counter = 8
                THEN datediff(mm, Q3.y_start, Q3.y_end) + 1
            ELSE 0
            END) Y9
    ,sum(CASE 
            WHEN Q3.Year_Counter = 9
                THEN datediff(mm, Q3.y_start, Q3.y_end) + 1
            ELSE 0
            END) Y10
FROM (
    SELECT Q1.[Begin Date]
        ,Q1.[End Date]
        ,Q1.years Diff_in_Year
        ,Q2.number AS Year_Counter
        ,(
            CASE 
                WHEN Q2.number = 0
                    THEN Q1.[Begin Date]
                ELSE dateadd(yy, datediff(yy, 0, dateadd(yy, q2.number, q1.[Begin Date])), 0)
                END
            ) AS y_Start
        ,(
            CASE 
                WHEN ((Q1.years - 1) = Q2.number)
                    THEN Q1.[End Date]
                ELSE DATEADD(yy, DATEDIFF(yy, 0, dateadd(yy, q2.number + 1, q1.[Begin Date]) + 1), - 1)
                END
            ) AS y_End
        ,Year(Q1.[Begin Date]) + Q2.number YearInYYYY
    FROM (
        SELECT [Begin Date]
            ,[End Date]
            ,DATEDIFF(year, [Begin Date], [End Date]) + 1 AS years
        FROM my dates
        ) Q1
    INNER JOIN master..spt_values Q2 ON Q2.type = 'P'
        AND Q2.number < Q1.years
    ) Q3
GROUP BY Q3.[Begin Date]
    ,Q3.[End Date]
    ,q3.Diff_in_Year

当前代码是如何工作的:给定一个日期范围,两个日期之间每年的月数。IE 1/1/2014 - 1/18/2015 将给出两列“2014”和 2015”,2014 的值为 12,2015 的值为 1,表示指定日期之间有 13 个月。

What I am hoping to achieve is something similar to
Start Date    End Date  Month 1   Month 2   Month 3
-----------------------------------------------------
1/1/2014      3/8/2014      4        4         1 
4

1 回答 1

1

除了动态 SQL 解决方案(在 TSQL 中搜索动态枢轴)之外,我还提出了几个答案。由于您的问题不清楚您需要数周还是数月,因此我为每个问题整理了一个快速的问题。

月份示例

declare @startdate date = '1/1/2014', @enddate date = '3/1/2015'
select p.*
from (
    select @startdate as StartDate, @enddate as EndDate, right(convert(varchar,(dateadd(mm,RowID-1,@startdate)),105),4) [Group]
    from (
        select *, row_number()over(order by name) as RowID
        from master..spt_values
        ) d
    where d.RowID <= datediff(mm, @startdate, @enddate)
    ) t
    pivot (
        count([Group]) for [Group] in (
            [2014],[2015]
        )
    ) p

示例在这里

declare @startdate date = '1/1/2014', @enddate date = '3/1/2015'
select p.*
from (
    select @startdate as StartDate, @enddate as EndDate, right(convert(varchar,(dateadd(ww,RowID-1,@startdate)),105),7) [Group]
    from (
        select *, row_number()over(order by name) as RowID
        from master..spt_values
        ) d
    where d.RowID <= datediff(ww, @startdate, @enddate)
    ) t
    pivot (
        count([Group]) for [Group] in (
              [01-2014]
            , [02-2014]
            , [03-2014]
            , [04-2014]
            , [05-2014]
            , [06-2014]
            , [07-2014]
            , [08-2014]
            , [09-2014]
            , [10-2014]
            , [11-2014]
            , [12-2014]
            , [01-2015]
            , [02-2015]
            , [03-2015]
        )
    ) p
于 2014-08-19T22:10:34.100 回答