枢轴查询的交换 row/column

Swap row/column of pivot query

我想知道如何翻转此查询的 rows/columns:

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

本次查询returns结果是这样的:

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,但我无法应用它。

简单地用年份值代替月份:

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