如何将多个列转换为行

时间:2016-06-28 09:35:12

标签: sql sql-server

我有一个示例查询数据表:

Sample data

我希望我的输出表看起来像:

Output sample data

我尝试使用pivot运算符,但不能将它们全部加起来。

1 个答案:

答案 0 :(得分:0)

SELECT 
    * 
FROM
    (
        SELECT * FROM woddb.cityStock_Unpivot
    ) AS tblCity
    UNPIVOT
    (
        OutputValue FOR [OutputFieldName] IN (Sales, stock, [Target])
    ) AS UNPVT
    PIVOT
    (
        SUM(OutputValue)
        For Category IN (Panel,ac,ref)
    ) AS PVT