按年度SQL多维度量标准的数据透视表

时间:2015-03-25 12:50:24

标签: sql sql-server pivot

有没有办法在1个SQL pivot操作符中显示多个指标。基本上,我有Table1,希望得到的结果是Table2格式。

Table1

ACCOUNTS    YEAR  REVENUE   MARGIN
ACCOUNT1    2012    100      50
ACCOUNT1    2013    104      52
ACCOUNT1    2014    108      54
ACCOUNT2    2012    112      56
ACCOUNT2    2013    116      58
ACCOUNT2    2014    120      60
ACCOUNT3    2012    124      62
ACCOUNT3    2013    128      64
ACCOUNT3    2014    132      66

Table2
ACCOUNTS     REVENUE_2012     REVENUE_2013     REVENUE_2014     MARGIN_2012     MARGIN_2013     MARGIN_2014     
ACCOUNT1     100              104              108              50              52              54
ACCOUNT2     112              116              120              56              58              60
ACCOUNT3     124              128              132              62              64              66

请帮助

3 个答案:

答案 0 :(得分:1)

你可以这样做:

SELECT
    Table1.ACCOUNTS,
    SUM(CASE WHEN Table1.[YEAR]=2012 THEN Table1.REVENUE ELSE 0 END) AS REVENUE_2012,
    SUM(CASE WHEN Table1.[YEAR]=2013 THEN Table1.REVENUE ELSE 0 END) AS REVENUE_2013,
    SUM(CASE WHEN Table1.[YEAR]=2014 THEN Table1.REVENUE ELSE 0 END) AS REVENUE_2014,
    SUM(CASE WHEN Table1.[YEAR]=2012 THEN Table1.MARGIN ELSE 0 END) AS MARGIN_2012,
    SUM(CASE WHEN Table1.[YEAR]=2013 THEN Table1.MARGIN ELSE 0 END) AS MARGIN_2013,
    SUM(CASE WHEN Table1.[YEAR]=2014 THEN Table1.MARGIN ELSE 0 END) AS MARGIN_2014
FROM
    Table1
GROUP BY
    Table1.ACCOUNTS

答案 1 :(得分:1)

DECLARE @t TABLE
    (
      ACCOUNTS NVARCHAR(MAX) ,
      YEAR INT ,
      REVENUE INT ,
      MARGIN INT
    )

INSERT  INTO @t
VALUES ('ACCOUNT1', 2012, 100, 50 ),('ACCOUNT1', 2013, 104, 52 ),
       ('ACCOUNT1', 2014, 108, 54 ),('ACCOUNT2', 2012, 112, 56 ),
       ('ACCOUNT2', 2013, 116, 58 ),('ACCOUNT2', 2014, 120, 60 ),
       ('ACCOUNT3', 2012, 124, 62 ),('ACCOUNT3', 2013, 128, 64 ),
       ('ACCOUNT3', 2014, 132, 66 )

;WITH CTE AS
(
  SELECT ACCOUNTS, value, name + '_' + cast(YEAR as char(4)) header
  FROM @t as p  
  UNPIVOT      
  (value FOR name IN           
  ([REVENUE], [MARGIN]) ) AS unpvt  
)
   SELECT ACCOUNTS, [REVENUE_2012],[REVENUE_2013],[REVENUE_2014]
         ,[MARGIN_2012],[MARGIN_2013],[MARGIN_2014] 
   FROM CTE
   PIVOT
   (SUM([value])  
   FOR header
   in([REVENUE_2012],[REVENUE_2013],[REVENUE_2014], [MARGIN_2012]
      ,[MARGIN_2013],[MARGIN_2014])  
   )AS p ORDER BY 2,3,4

结果:

ACCOUNTS REVENUE_2012 REVENUE_2013 REVENUE_2014 MARGIN_2012 MARGIN_2013 MARGIN_2014
ACCOUNT1 100          104          108          50          52          54
ACCOUNT2 112          116          120          56          58          60
ACCOUNT3 124          128          132          62          64          66

答案 2 :(得分:0)

DECLARE @t TABLE
    (
      ACCOUNTS NVARCHAR(MAX) ,
      YEAR INT ,
      REVENUE INT ,
      MARGIN INT
    )

INSERT  INTO @t
VALUES  ( 'ACCOUNT1', 2012, 100, 50 ),
        ( 'ACCOUNT1', 2013, 104, 52 ),
        ( 'ACCOUNT1', 2014, 108, 54 ),
        ( 'ACCOUNT2', 2012, 112, 56 ),
        ( 'ACCOUNT2', 2013, 116, 58 ),
        ( 'ACCOUNT2', 2014, 120, 60 ),
        ( 'ACCOUNT3', 2012, 124, 62 ),
        ( 'ACCOUNT3', 2013, 128, 64 ),
        ( 'ACCOUNT3', 2014, 132, 66 );
WITH    cte1
          AS ( SELECT   ACCOUNTS ,
                        YEAR ,
                        REVENUE
               FROM     @t
             ),
        cte2
          AS ( SELECT   ACCOUNTS ,
                        YEAR ,
                        MARGIN
               FROM     @t
             )
    SELECT  a1.* ,
            a2.[2012] ,
            a2.[2013] ,
            a2.[2014]
    FROM    ( SELECT    *
              FROM      cte1 PIVOT( SUM(REVENUE) FOR YEAR IN ( [2012], [2013], [2014] ) ) AS p
            ) a1
            JOIN ( SELECT   *
                   FROM     cte2 PIVOT( SUM(MARGIN) FOR YEAR IN ( [2012], [2013], [2014] ) ) AS p
                 ) a2 ON a1.ACCOUNTS = a2.ACCOUNTS

输出:

ACCOUNTS    2012    2013    2014    2012    2013    2014
ACCOUNT1    100     104     108     50      52      54
ACCOUNT2    112     116     120     56      58      60
ACCOUNT3    124     128     132     62      64      66