I’ve got two MS Access tables.
table1
ID Country Dat Val
1 Australia 01.10.2021 10
2 Canada 01.10.2021 100
3 Japan 01.10.2021 1000
4 Australia 02.10.2021 20
5 Canada 02.10.2021 200
6 Japan 02.10.2021 2000
and
table2
ID Country Dat Val
1 Canada 01.10.2021 50000
2 Greece 01.10.2021 50100
3 Canada 02.10.2021 60000
4 Greece 02.10.2021 60100
This SQL-code allows me to merge two tables, which gives 10 rows.
SELECT table2.Dat,
table2.Country,
table2.Val
FROM table2
LEFT JOIN table1
ON (table1.Dat = table2.Dat)
AND (table1.Country = table2.Country)
UNION ALL
SELECT table1.Dat,
table1.Country,
table1.Val
FROM table1
LEFT JOIN table2
ON (table1.Dat = table2.Dat)
AND (table1.Country = table2.Country)
ORDER BY table2.Dat,
table2.Country;
And it gives duplicates based on "Date" and "Country" criteria.
The question
I wonder, what should the code look like if I would like to merge two tables in a way that, if table1 and table2 have the same "Date" and "Country", then table2 rows would be chosen, not both?
In other words, how to take all unique rows from table1 and table2 that doesn’t match criteria "Date" AND "Country", as well as give precedence to table2 when rows in two tables match the criteria "Date" AND "Country"?
(A "Date + Country" bundle is unique within each table, i.e. there is only one "Data + Country" within one table)
In other words, what query could I use to make the result look like this, i.e. 8 rows without date + country duplicates?
Expected Result
Dat Country Val
01.10.2021 Australia 10
01.10.2021 Canada 50000
01.10.2021 Greece 50100
01.10.2021 Japan 1000
02.10.2021 Australia 20
02.10.2021 Canada 60000
02.10.2021 Greece 60100
02.10.2021 Japan 2000
And what might be a good way to make such SQL-scripts to merge tables based upon several criteria like this?