I have a table with in there following data
ID|LabelID|Value
1 |1 |3
1 |2 |1
1 |3 |15
2 |1 |5
2 |2 |7
2 |3 |5
I want to get the following as result within a pervasive database
ID|Label1|Label2|Label3
1 |3 |1 |15
2 |5 |7 |5
Anyone an idea? I did try some things and the best result I can get is the following:
ID|Label1|Label2|Label3
1 |3 | |
1 | |1 |
1 | | |15
2 |5 | |
2 | |7 |
2 | | |5
Have fun... :-)
Should work in most versions of SQL. There are vendor specific PIVOT statements which are also an option.
This was the way to do it prior to PIVOT clause.
drop table #test;
create table #test (ID int, LabelID int, Value int);
insert into #test values (1, 1, 3)
,(1, 2, 1)
,(1, 3, 15)
,(2, 1, 5)
,(2, 2, 7)
,(2, 3, 5);
select ID
,sum(case when LabelID = 1 then Value else null end) as Label1
,sum(case when LabelID = 2 then Value else null end) as Label2
,sum(case when LabelID = 3 then Value else null end) as Label3
from #test
group by ID;