SQL转置数据 [英] SQL to transpose data
本文介绍了SQL转置数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个名为Forecast的表,如下所示:
I have a table named Forecast as below:
Product_BK大约有80,000条记录,而本月则有12条记录.状态保持不变,为5.
Product_BK has about 80,000 records, while the Month has 12. The States remain static at 5.
如何编写查询以转置数据,如下所示:
How can I write a query to transpose the data to look like below:
这是在SQL Server上,不希望使用动态查询路由.
This is on a SQL server and prefer not to go down the dynamic query route.
感谢您的所有投入
推荐答案
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
这篇关于SQL转置数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文