I have a table called ‘MyTable’ that looks like:
ID | Item | Type | Value
1 | 100 | 60 | 37
2 | 100 | 72 | 24
3 | 200 | 60 | 18
4 | 300 | 60 | 43
5 | 300 | 72 | 76
And I would like to put together an MS SQL query to obtain the following:
Item | Type_60 | Type_72
100 | 37 | 24
200 | 18 | NULL
300 | 43 | 76
most probably I am not allowed to use PIVOT (that anyway I did not understand how to use for this purpose!).
So far I could only come out with the following:
select Item,
CASE Type WHEN 60 THEN Value END as 'Type_60',
CASE Type WHEN 72 THEN Value END as 'Type_72'
from MyTable
but this gives me the following:
Item | Type_60 | Type_72
100 | 37 | NULL
100 | NULL | 24
200 | 18 | NULL
300 | 43 | NULL
300 | NULL | 76
Can anyone hlep me?
You’re almost there, just add
group by Itemat the end, and wrap thecasein an aggregate: