I would like to identify records that are in table #1 but not in table #2 in a situation where there are three composite keys to determine if a record is unique. The below code is as close as I get but the where clause needs work.
In this test case the results includes:
(2, 3, 2, null)
(4, 4, 1, null)
(7, 2, 2, null)
(8, 1, 2, null)
CREATE TABLE OldReports (
REPORT_ID int,
USER_ID int,
CLIENT_ID int,
MY_DATA varchar(100)
)
INSERT INTO OldReports
(REPORT_ID, USER_ID, CLIENT_ID, MY_DATA)
VALUES
(1, 1, 2, null),
(6, 3, 3, null),
(6, 4, 4, null),
(5, 1, 2, null),
(5, 1, 3, null),
(7, 2, 1, null),
(8, 1, 1, null)
CREATE TABLE NewReports (
REPORT_ID int,
USER_ID int,
CLIENT_ID int,
MY_DATA varchar(100)
)
INSERT INTO NewReports
(REPORT_ID, USER_ID, CLIENT_ID, MY_DATA)
VALUES
(1, 1, 2, null),
(2, 3, 2, null),
(4, 4, 1, null),
(5, 1, 2, null),
(5, 1, 3, null),
(7, 2, 2, null),
(8, 1, 2, null)
DROP TABLE IF EXISTS #ReportDifferences
SELECT DISTINCT
REPORT_ID,
USER_ID,
CLIENT_ID
INTO #ReportDifferences
FROM NewReports n
WHERE (IF NOT EXISTS (
SELECT *
FROM OldReports o
WHERE
n.REPORT_ID = o.REPORT_ID and
n.USER_ID = o.USER_ID and
n.CLIENT_ID = o.CLIENT_ID)
)
-- tell me what lives in NewReports but not in OldReports
SELECT * FROM #ReportDifferences
I attempted this solution (How to select all records from one table that do not exist in another table?) but on a large data set, the results are not mutually exclusive. Something like:
LEFT JOIN OldReports o ON u.CLIENT_ID = o.CLIENT_ID AND u.REPORT_ID = o.REPORT_ID AND u.USER_ID = o.USER_ID
WHERE
u.CLIENT_ID is null
AND u.REPORT_ID is null
AND u.USER_ID is null