sql >> Base de Datos >  >> RDS >> Sqlserver

Transponer filas a columnas según la columna de ID

puede usar la cláusula pivote de SQL Server para esto:

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

o puede pivotar manualmente:

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

demostración de sql fiddle