且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

根据 ID 列将行转置为列

更新时间:2023-11-18 21:47:10

您可以为此使用 SQL Server 数据透视子句:

you can use SQL Server pivot clause for this:

select
    p.*
from Table1
pivot(
    max([Field Selection])
    for [Field Name] in ([Rating 1], [Rating 2], [Rating 3])
) as p

或者您可以手动旋转:

select
    ID,
    max(case when [Field Name] = 'Rating 1' then [Field Selection] end) as [Rating 1], 
    max(case when [Field Name] = 'Rating 2' then [Field Selection] end) as [Rating 2],
    max(case when [Field Name] = 'Rating 3' then [Field Selection] end) as [Rating 3]
from Table1
group by ID

sql 小提琴演示