交换数据透视查询的行/列

时间:2015-05-07 12:27:19

标签: sql sql-server pivot unpivot

我想知道如何翻转此查询的行/列:

SELECT *
FROM (SELECT YEAR(DATA_DOC) AS YEAR, DATENAME(MONTH, DATA_DOC) AS MONTH, SUM(IMP) AS TOTAL
        FROM MyTable
        WHERE YEAR(DATA_DOC) BETWEEN 2011 AND 2014
        GROUP BY YEAR(DATA_DOC), DATENAME(MONTH, DATA_DOC)
    ) AS REPORT
PIVOT
(
    SUM(TOTAL) FOR MONTH IN (January,....ecc...)
) AS REPORT_T

此查询以这种方式返回结果:

Year       |    January    |     February      |      ecc.....
2011       |     1000      |        500        |      ....
2012       |      250      |        1110       |       ....
2013       |      0        |        150        |       .... 
2014       |     880       |        450        |       ....

我可以反转列和行,以便得到:

Month      |    2011       |        2012       |       2013
January    |     1000      |        500        |      ....
February   |      250      |        1110       |       ....
...        |      0        |        150        |       .... 
...        |     880       |        450        |       ....

我应该使用UNPIVOT,但我无法应用它。

1 个答案:

答案 0 :(得分:1)

简单地将年份值替换为月份:

SELECT *
FROM (SELECT YEAR(DATA_DOC) AS YEAR, DATENAME(MONTH, DATA_DOC) AS MONTH, SUM(IMP) AS TOTAL
      FROM MyTable
      WHERE YEAR(DATA_DOC) BETWEEN 2011 AND 2014
      GROUP BY YEAR(DATA_DOC), DATENAME(MONTH, DATA_DOC)
    ) AS REPORT
PIVOT
(
    SUM(TOTAL) FOR YEAR IN ([2011],[2012],[2013],[2014])
) AS REPORT_T

SQL Fiddle Demo

如果您想要生成12行,每月一行,即使某些月份没有可用数据,您也可以将上述查询修改为:

 SELECT x.m, t.[2011], t.[2012], t.[2013], t.[2014]
 FROM (VALUES ('January'), ('February'), ('March'), ('April'), ...etc) x(m)
 LEFT JOIN (
   SELECT *
   FROM (SELECT YEAR(DATA_DOC) AS YEAR, 
                DATENAME(MONTH, DATA_DOC) AS MONTH, 
                SUM(IMP) AS TOTAL
         FROM MyTable
         WHERE YEAR(DATA_DOC) BETWEEN 2011 AND 2014
         GROUP BY YEAR(DATA_DOC), DATENAME(MONTH, DATA_DOC)
        ) AS REPORT
   PIVOT
   (
       SUM(TOTAL) FOR YEAR IN ([2011],[2012],[2013],[2014])
   ) AS REPORT_T ) AS t ON x.m = t.MONTH

SQL Fiddle Demo