Reputation: 8146
I hope it's clear. How can I achieve the result showed in the following image?
Upvotes: 0
Views: 1966
Reputation: 52655
Just do an outer join but also use coalesce
SELECT
COALESCE(a.master,b.master) master,
a.ColA,
b.ColB
FROM
a FULL OUTER JOIN B
ON a.master = b.master
AND (a.ColA = b.ColB
or a.ColA is Null
or b.ColB is Null)
ORDER BY
COALESCE(a.master,b.master),
COALESCE(a.cola,b.colB)
See it working here
Upvotes: 3
Reputation: 65274
SELECT
masters.master AS master,
TableA.ColA AS ColA,
TableB.ColB AS ColB,
FROM
(
SELECT master FROM TableA
UNION
SELECT master FROM TableB
) AS masters
LEFT JOIN TableA ON masters.master=TableA.master
LEFT JOIN TableB ON masters.master=TableB.master
ORDER BY
master, ColA, ColB
Upvotes: 1