使用PIVOT日期转换为MTD,QTD,YTD

时间:2015-03-10 17:26:43

标签: sql-server pivot unpivot

我有一张类似于下面的表格

    Dates Start Date End    Code    Type    Values
    Jan-14  Dec-14  AccountCode A1  0.005
    Oct-14  Dec-14  AccountCode A1  0.001
    Dec-14  Dec-14  AccountCode A1  0.0017
    Jan-14  Dec-14  AccountCode A2  -0.004832912
    Oct-14  Dec-14  AccountCode A2  -0.002195574
    Dec-14  Dec-14  AccountCode A2  -0.001396166

需要编写一个查询,我可以使用pivot转换为

displayperiod   A1          A2
YTD            0.005    -0.004832912
QTD            0.001    -0.002195574
MTD            0.0017   -0.001396166

我目前只是将日期转换为年初至今,MTD和QTD。

非常感谢任何帮助。

********************添加更多数据项 Jan的数据

        SELECT * FROM mytable
        WHERE   
        AND data_enddate = '20140131' 
        ORDER BY type, data_startdate 

        Output from table

        data_startdate  data_enddate    factor_name value
        1/1/2014    1/31/2014   A1  0.0018  
        1/1/2014    1/31/2014   A2  0.0024 

        In this case MTD = YTD = QTD

        hence the output required

        displayperiod   A1          A2
        YTD            0.0018    0.002
        QTD            0.0018    0.002
        MTD            0.0018   0.002

2月的数据

        SELECT * FROM mytable
        WHERE   
        AND data_enddate = '20140228' 
        ORDER BY type, data_startdate 

        Output from table

        data_startdate  data_enddate    factor_name value
        1/1/2014        2014-02-28      A1  0.0011  
        1/2/2014        2014-02-28      A1  0.0024 
        1/1/2014        2014-02-28      A2  0.0021  
        1/2/2014        2014-02-28      A2  0.0034 

        In this case MTD = QTD ,YTD 

        hence the output required

        displayperiod   A1          A2
        YTD            0.0024    0.0034
        QTD            0.0011    0.0021
        MTD            0.0011    0.0021

Mar的数据

        SELECT * FROM mytable
        WHERE   
        AND data_enddate = '20140331' 
        ORDER BY type, data_startdate 

        Output from table

        data_startdate  data_enddate    factor_name value
        1/1/2014        2014-03-31      A1  0.0011  
        1/3/2014        2014-03-31      A1  0.0024 
        1/1/2014        2014-02-28      A2  0.0021  
        1/2/2014        2014-02-28      A2  0.0034 

在这种情况下MTD,QTD = YTD

        hence the output required

        displayperiod   A1          A2
        YTD            0.0011    0.0021
        QTD            0.0011    0.0021
        MTD            0.0024    0.0034

4月的数据

            SELECT * FROM mytable
            WHERE   
            AND data_enddate = '2014-04-30' 
            ORDER BY type, data_startdate 

            Output from table

            data_startdate  data_enddate    factor_name value
            1/1/2014        2014-04-30      A1  0.0011  
            1/4/2014        2014-04-30      A1  0.0024 
            1/1/2014        2014-04-30      A2  0.0021  
            1/4/2014        2014-04-30      A2  0.0034 

            In this case MTD , QTD=YTD 

            hence the output required

            displayperiod   A1          A2
            YTD            0.0011    0.0021
            QTD            0.0011    0.0021
            MTD            0.0024    0.0034

五月的数据

            SELECT * FROM mytable
            WHERE   
            AND data_enddate = '2014-04-30' 
            ORDER BY type, data_startdate 

            Output from table

            data_startdate  data_enddate    factor_name value
            1/1/2014        2014-05-31      A1  0.0011  
            1/4/2014        2014-05-31      A1  0.0024 
            1/5/2014        2014-05-31      A1  0.0030
            1/1/2014        2014-05-31      A2  0.0021  
            1/4/2014        2014-05-31      A2  0.0034 
            1/5/2014        2014-05-31      A2  0.0032 

            In this case MTD , QTD,YTD 

            hence the output required

            displayperiod   A1          A2
            YTD            0.0011    0.0021
            QTD            0.0024    0.0034
            MTD            0.0030    0.0032

六月的数据

六月将有3行

7月的数据

7月将有2行,其中MTD = QTD并且分开YTD

8月的数据

AUG将有2行,其中MTD = QTD并且分开YTD

9月数据 SEPt将有3行

10月的数据 Oct将有2行,其中MTD = QTD并且分开YTD

NOV数据

11月将有3行

DEC的数据

Dec将有3行

2 个答案:

答案 0 :(得分:2)

month源查询中查找startend日期之间的pivot差异,以使用displayperiod语句获取case。试试这个。

;WITH cte
     AS (SELECT Cast('01-' + Dates_Start AS DATE) Date_Start,
                Cast('01-' + Date_End AS DATE)    Date_End,
                code,type,value
         FROM   yourtable)
SELECT CASE
         WHEN Datediff(mm, Date_Start, Date_End) + 1 = 12 THEN 'YTD'
         WHEN Datediff(mm, Date_Start, Date_End) + 1 = 3 THEN 'QTD'
         WHEN Datediff(mm, Date_Start, Date_End) + 1 = 1 THEN 'MTD'
       END AS displayperiod,
       [A1],
       [A2]
FROM   cte
       PIVOT (Max(value)
             FOR type IN ([A1],
                          [A2]))pv 

FIDDLE DEMO

答案 1 :(得分:0)

这里有效的答案

                    SELECT * INTO #temp FROM (
                     SELECT startdate  ,
                                    endate ,
                                    factor_name,contribution
                             FROM   myTable WHERE account_code = 'myaccount'  and endate = '20140228' AND startdate IN ( SELECT
                       DATEADD(yy, DATEDIFF(yy,0,'20140228'), 0) AS StartOfYear)

                       UNION ALL

                        SELECT startdate  ,
                                    endate ,
                                    factor_name,contribution
                             FROM   myTable WHERE account_code = 'myaccount'  and endate = '20140228' AND startdate IN ( 
                    SELECT   
                        DATEADD(q, DATEDIFF(q, 0, '20140228'), 0) )


                       UNION ALL 

                        SELECT startdate  ,
                                    endate ,
                                    factor_name,contribution
                             FROM   myTable WHERE account_code = 'myaccount'  and endate = '20140228' AND startdate IN (SELECT DATEADD(mm, DATEDIFF(mm, 0, '20140228'), 0) )

                             ) AS tmp