sql - How to use union in select clause? -


i have 2 tables (created here example) like:

x1   y1   1 b   2 c   3 d   4  

and

x2   y2    5 m    6 n    7 b    4 

and want output column:

x    y1   y2    1    5 b    2    4 c    3    0 d    4    0 m    0    6 n    0    7 

what tried is:

select (a.x1 union b.x1) x, a.y1,b.y2 full outer join b on a.x1 = b.x2 

(the query mentioned above sample). after executing query getting error message:

syntax error: near union in select clause

can tell me wrong here. there other option output table in mentioned format?

union used join results 1 after another. you're attempting join results side side (which did!). thing you're missing coalesce call handle missing values:

select coalesce(a.x1, b.x2) x,         coalesce(a.y1, 0) y1,         coalesce(b.y2, 0) y2    full outer join b on a.x1 = b.x2 

Comments