Skip to content
Advertisement

Find records that do not exist in 2nd table, and also the values mapped against them from first

Hi so this question has been asked before, but I am not only just trying to extract the records that do not just exist in the first table, but also the mapping from the first table where they don’t?

Eg.

Assuming I will join using Month and Year–

Table 1:

Month Year
3 2021
4 2021
5 2021
6 2023

Table 2:

ID Month Year
11 3 2021
22 4 2021
33 5 2021

Output:

ID Month Year
11 6 2023
22 6 2023
33 6 2023

Advertisement

Answer

From what you say and show, you want to cross join all table 2 rows to the table 1 rows that don’t have a match in table 2. For the lookup you can use NOT EXISTS.

select t2.id, t1.month, t1.year
from t1 cross join t2
where not exists
(
  select null
  from t2
  where t2.year = t1.year and t2.month = t1.month
);
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement