Reputation: 339
I have data in following format
ID Loss Sum
--------------------------
1 146276293.1 1
1 175538865.5 2
1 146276293.1 3
I want SQL script to return me
ID Sum1 Sum2 Sum3
---------------------------------------------------
1 146276293.1 175538865.5 146276293.1
Upvotes: 1
Views: 63
Reputation: 1032
This simple example does what you're trying to do. PIVOT is a great tool. Also research UNPIVOT when attempting the opposite.
select *
from (
select 1 id, 100 num, 'Sum1' col
union select 1, 200, 'Sum2'
union select 1, 300, 'Sum3' ) x
pivot
(sum(num) for col in (Sum1, Sum2, Sum3)) p
Upvotes: 1