Question

I have the following tables:

 T         T  A  B          T  A  C
---      -----------      -----------
 1         1  x  y          2  x  z
 2        
 3

And now i would like to join them and get the following one:

T    A    B    C
------------------
1    x    y   null
2    x   null  z
3   null null null

Is that possible in SQL?

Thanks alot !

Was it helpful?

Solution

You can use LEFT OUTER JOINS, using your first table as the left table. The coalesce is there to resolve / best effort on your assumed duplicated column A (value 'x').

SELECT t.T, COALESCE(tab.A, tac.a) as A, tab.B, tac.C
FROM T t
LEFT OUTER JOIN SecondTable tab
ON tab.T = t.T
LEFT OUTER JOIN ThirdTable tac
ON tac.T = t.T;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top