fosterXO
fosterXO

Reputation: 33

SQL join filling null columns

I have two tables where I need to do a FULL JOIN. Table2 has many missing values that can be found in table1.

I need to combine (if column1, 2 and 3 are the same) and append the information from table2 filling the null values from table1.

TABLE1

Column1 Column2 Column3 measure1 measure2
A B DAY1 50 null
A B DAY2 10 null

TABLE 2

Column1 Column2 Column3 measure1 measure2
A B DAY1 null 100
A null DAY3 null 300

DESIRED RESULT

Column1 Column2 Column3 measure1 measure2
A B DAY1 50 100
A B DAY2 10 null
A B DAY3 null 300

In this case, I combined first rows from table1 and table2. For second row in table2 we lookup the value of column2 from table1.1.

Upvotes: 0

Views: 831

Answers (1)

Slava Rozhnev
Slava Rozhnev

Reputation: 10163

You can use FULL OUTER JOIN here:

select 
    coalesce(t1.id, t2.id) id,
    coalesce(t1.c1, t2.c1) c1,
    coalesce(t1.c2, t2.c2) c2
from t1
full outer join t2 on t1.id = t2.id;

SQLize fiddle

Upvotes: 0

Related Questions