SQL to transpose data

后端 未结 1 746
栀梦
栀梦 2021-01-22 15:43

I have a table named Forecast as below:

Product_BK has about 80,000 records, while the Month has 12. The States remain static at 5.

How can I write a qu

1条回答
  •  执笔经年
    2021-01-22 16:26

    You can use SQL Server's built-in UNPIVOT function:

    SELECT PRODUCT_BK, Month, State, Forecast
    FROM
    (SELECT * FROM Forecast_Table) t
    UNPIVOT
    (Forecast FOR State IN (SA_NT, QLD, VIC_TAS, WA, NSW_ACT)) AS fcst
    

    0 讨论(0)
提交回复
热议问题