问题描述
我对 SQL 很陌生.
I am very new to SQL.
我有一张这样的桌子:
我被告知要获取这样的数据
And I was told to get data like this
我知道我需要使用 PIVOT 功能.但是不能清楚的理解.如果有人可以在上述情况下对其进行解释,那将是非常有帮助的.(或任何替代方案)
I understand I need to use PIVOT function. But can't understand it clearly. It would be great help if somebody can explain it in above case.(or any alternatives if any)
推荐答案
A PIVOT
用于将数据从一列旋转到多列.
A PIVOT
used to rotate the data from one column into multiple columns.
对于您的示例,这里是一个 STATIC Pivot,这意味着您对要旋转的列进行了硬编码:
For your example here is a STATIC Pivot meaning you hard code the columns that you want to rotate:
这是一个带有工作版本的 SQL 演示.
Here is a SQL Demo with a working version.
这也可以通过动态 PIVOT 来完成,您可以在其中动态创建列列表并执行 PIVOT.
This can also be done through a dynamic PIVOT where you create the list of columns dynamically and perform the PIVOT.
两者的结果:
这篇关于理解 T-SQL 中的 PIVOT 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!