I have a table of format
id name flag
----------------
11 Jack 1
11 Jill 2
23 Joe 1
23 John 2
23 Sam 1
46 White 2
46 Dan 2
57 Dave 1
I am trying to return the table of format (based on the flag for particular id) like
id name_1 name_2
------------------
11 Jack Jill
23 Joe John
23 Sam John
46 NULL White
46 NULL Dan
57 Dave NULL
I tried this query but it does not give the expected result.
select id,
case flag when 1 then name end as name_1,
case flag when 2 then name end as name_2
from temp;
You want a join, not a select case. Something like this, assuming null is possible in either column:
Not sure what you expect in this case though: