Skip to content
Advertisement

Full Join on two tables with criteria on both

I have two tables with a matching key. Some records exist on both tables, others only exist on one or the other. What I cannot for the life of me figure out is how to add criteria to both tables when doing a full outer join.

Table A (EDIT: with criteria)

Table B (EDIT: with criteria)

Desired Join

So far I have the following code. It is returning everything for me besides ID5 and ID6, for some reason it won’t bring rows in the join where col in Table A are null (n/a).

Am I putting my criteria in the incorrect locations? How can I do a Full Join but also apply criteria to both TableA and TableB ?

Advertisement

Answer

To limit the scope of the full join I suggest that you use subqueries to implement the wanted conditions using where clauses, such as this:

ID   | AREA | STATUS | YEAR | ID   | ZONE | CODE | TIME 
:--- | :--- | :----- | ---: | :--- | :--- | :--- | :----
ID1  | AA   | YES    | 1980 | ID1  | FF   | True | 12:00
ID2  | BB   | NO     | 1990 | null | null | null | null 
ID3  | CC   | YES    | 1950 | null | null | null | null 
ID4  | DD   | NO     | 1900 | null | null | null | null 
null | null | null   | null | ID5  | HH   | True | 11:11
null | null | null   | null | ID6  | II   | True | 13:00
ID   | AREA | STATUS | YEAR | ID   | ZONE | CODE | TIME 
:--- | :--- | :----- | ---: | :--- | :--- | :--- | :----
ID1  | AA   | YES    | 1980 | ID1  | FF   | True | 12:00
null | null | null   | null | ID5  | HH   | True | 11:11
null | null | null   | null | ID6  | II   | True | 13:00
null | null | null   | null | ID6  | II   | True | 01:30
ID2  | BB   | NO     | 1990 | null | null | null | null 
ID3  | CC   | YES    | 1950 | null | null | null | null 

db<>fiddle here

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement