'SQL - Keep only columns that have some field that don't match
I'm updating a table daily, but I only want to update the rows that had any value modified from the previous day.
For example, I have 2 tables (Today and Yesterday):
Today:
| ID | NAME | MONTH | COUNTRY | YEAR |
|---|---|---|---|---|
| 1 | Sta | April | Jamaica | 1992 |
| 2 | Danny | November | France | 1982 |
| 3 | Elle | June | Australia | 2020 |
| 4 | John | April | Uruguay | 1765 |
| 5 | Paul | May | Canada | 1990 |
Yesterday:
| ID | NAME | MONTH | COUNTRY | YEAR |
|---|---|---|---|---|
| 1 | Sta | April | Jamaica | 1992 |
| 2 | Danny | November | Spain | 1982 |
| 3 | Elle | July | Australia | 2022 |
| 5 | Paul | May | Canada | 1990 |
So I want to create a table that only has ID 2, 3 and 4 from Today, because 1 or more fields changed, or the row appears in table Today but it doesn't appear in table Yesterday:
| ID | NAME | MONTH | COUNTRY | YEAR |
|---|---|---|---|---|
| 2 | Danny | November | France | 1982 |
| 3 | Elle | June | Australia | 2020 |
| 4 | John | April | Uruguay | 1765 |
I tried to use the MINUS function, but I'm using MS-Access and it doesn't work. There are +100 columns, so I can't do something like: WHERE Today.ID = Yesterday.ID AND (Today.NAME <> Yesterday.NAME OR Today.COUNTRY <> Yesterday.COUNTRY OR Today.YEAR <> Yesterday.YEAR)
Solution 1:[1]
You can use not exists:
select t.*
from today as t
where not exists (select 1
from yesterday as y
where y.id = t.id and y.name = t.name and
y.month = t.month and y.country = t.country and
y.year = t.year
);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Gordon Linoff |
