SQL complex dynamic Pivoting 2(SQL复杂动态透视2)
问题描述
您好,我正在 SQL Server 中尝试对下表进行透视
Hi I am trying in SQL Server the pivoting for the following table
我希望输出为
原始表中的列数已知,但行数未知.谁能帮忙
The number of columns in the original table are known but the number of rows are not known. Can any one help
推荐答案
如果您想将 sequence
编号作为列名的一部分,那么您仍然需要取消转置您的 col1
和 col2
列首先,然后应用枢轴.不同之处在于您会将 sequence
编号连接到在反透视过程中创建的列名.
If you want to include the sequence
number as part of your column names, then you will still need to unpivot your col1
and col2
columns first, then apply the pivot. The difference is that you will concatenate the sequence
number to your column names created during the unpivot process.
对于已知数量的值,查询将是:
For a known number of values the query would be:
如果你有一个未知的数字,动态 SQL 版本将是:
Then if you have an unknown number the dynamic SQL version will be:
这篇关于SQL复杂动态透视2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!