我有一个包含3列的表格,其中显示了每个月的产品收入:
1。产品
2。收入$
3.Month_Date
表就像:
Product | Month_Date | Revenue$
A 2013-01 10
A 2013-02 11
A 2013-03 8
... ... ...
A 2019-04 20
B 2013-01 2
B 2013-02 5
问题是从2013年1月到2019-04年有76个月,我想用列而不是行显示月份,并连续显示每个产品的收入,以便数据应该像下表:
Product | 2013-01 | 2013-02 | 2013-03 | ...... | 2019-04
Revenue$ A 10 11 8 ...... 20
B 2 5 3 ...... 6
请告诉我是否有任何有效的方法可以帮助我?我已经尝试了很长时间,没有解决这个问题,而不是进行硬编码。非常感谢您的提前帮助!
答案 0 :(得分:0)
对于Sql Server,您可以看到this example。
从页面提取:
SELECT *
FROM
(
SELECT [PolNumber],
[PolType],
[Effective Date],
[DocName],
[Submitted]
FROM [dbo].[InsuranceClaims]
) AS SourceTable PIVOT(AVG([Submitted]) FOR [DocName] IN([Doc A],
[Doc B],
[Doc C],
[Doc D],
[Doc E])) AS PivotTable;