简体   繁体   中英

SQL Server transform table pivoting

I have this table in SQL Server:

Id Type   Value
---------------
100 up    109
100 up    44
101 down  51
101 down  77
102 left  88
103 right 99
103 right 12

How can I transform it to look like this?

Id   Up   Down   Left   Right
-------------------------------
100  109   N/A    N/A     N/A
100   44   N/A    N/A     N/A
101   N/A   51    N/A     N/A
101   N/A   77    N/A     N/A
102   N/A   N/A    88     N/A
103   N/A   N/A   N/A      99
103   N/A   N/A   N/A      12

All you need are some Case statements:

select 
  Case When Type = 'Up' Then Value Else 'N/A' END as Up
  ,Case When Type = 'Down' Then Value Else 'N/A' END as Down

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM