sql >> Database >  >> RDS >> Sqlserver

Transponeer rijen naar kolommen op basis van ID-kolom

u kunt hiervoor de spilclausule van SQL Server gebruiken:

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

of u kunt handmatig draaien:

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 fiddle-demo




  1. Hoe OCT() werkt in MariaDB

  2. Kenmerken laden van gekoppelde modellen met sequelize.js

  3. Geen toegang tot SQL-gegevens buiten mijn while-lus

  4. Hoe sqlcmd &bcp op Ubuntu te installeren