SQL-日期差异 - 两个日期之间每个月的周数

时间:2014-08-19 12:38:41

标签: sql tsql date datediff date-difference

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

我目前在哪里:

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 - 2015年1月18日将提供两列&#34; 2014&#34;和2015&#34; 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 

1 个答案:

答案 0 :(得分:1)

除了动态SQL解决方案(在TSQL中搜索动态数据透视)之外,我还提出了几个答案。既然你的问题不清楚你是想要几个星期还是几个月,我就会为每个人安排一个快速的问题。

Example Here

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

Example Here

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