There are two tables.
table1
select tt1.*
from
(values(1,'key1',5),(2,'key1',6),(3,'key1',7)
) as tt1(id, field2,field3)
table2
select tt2.*
from
(values(4,'key1',null),(2,'key1',null),(3,'key1',null)) as tt2(id, field2,field3)
How can I combine these two tables in a query to get the result like this?
select tt2.*
from
(values(1,'key1',5),(2,'key1',6),(3,'key1',7),(4,'key1',null)) as tt2(id,field2,field3)
2
Answers
I think you need this SQL query:
Do a
full join
(orfull outer join
) on theid
column and usecoalesce
to merge the columns from the two tables:Output:
Note that if there are different non-null values in tt1 and tt2, with this
coalesce
call, tt1 values will be chosen.