Wednesday, January 21, 2015

SQL row merging


Given Table
ID  VAL1    VAL2
1     5          NULL
2     NULL     2
1     NULL     2
2     3          NULL

Expected Table
ID  VAL1 VAL2
1      5       2
2      3       2

 
Query
select * from 
(select distinct s1.id, s1.val1, s2.val2
from sample as s1
inner join sample as s2on s1.id = s2.id) t 
where t.val1 IS NOT NULL and t.val2 IS NOT NULL

No comments:

Post a Comment