First of all, the title of the question sucks... I hope the description below will clarify the situation.
The problem is as follows; consider this table:
id part1 part2 v
-------------------------
id1 p1 p2 v1old
id1 p3 p4 v2
id2 p1 p2 v1new
id2 p5 p6 v3
Now, I am given two values of the id
column; let's say id1
and id2
from the sample data above. The result I seek is:
part1 part2 vold vnew
------------------------------
p1 p2 v1old v1new
p3 p4 v2 NULL
p5 p6 NULL v3
After some investigation and the help of this question, I could build this query which does the trick:
select t1.part1, t1.part2, t1.v as vold, t2.v as vnew
from (select part1, part2, v from t where id = 1) t1
left join (select part1, part2, v from t where id = 2) t2
on t1.part1 = t2.part1 and t1.part2 = t2.part2
union
select t2.part1, t2.part2, t1.v as vold, t2.v as vnew
from (select part1, part2, v from t where id = 1) t1
right join (select part1, part2, v from t where id = 2) t2
on t1.part1 = t2.part1 and t1.part2 = t2.part2
;
The problem here is duplicates which union
will happily get rid of... But there are a lot of them to remove. Is there a version which can avoid generating those duplicates?