How to identify all rows in one table that are not in another table -- with a nuance

时间:2015-07-28 15:55:30

标签: sql sql-server

I am using SQL Server 2008. I am using the following technique to identify all rows in one table table_A that are not in another table table_B:

SELECT
table_A.key
FROM table_A
LEFT JOIN table_B
ON
table_A.key = table_B.key
WHERE
table_B.key IS NULL

I am actually identifying all rows in table_A that aren't in other tables, listed by different keys. For example,

SELECT
'col1|col2|col3' AS Columns_in_Question,
CASE WHEN table_B.colx IS NULL THEN 'col1 value not found' ELSE '' END + '|' +
CASE WHEN table_C.colphi IS NULL THEN 'col2 value not found' ELSE '' END + '|' +
CASE WHEN table_D.cola IS NULL THEN 'col3 value not found' ELSE '' END AS Error_Messages,
CASE WHEN table_B.colx IS NULL THEN table_A.col1 ELSE '' END + '|' +
CASE WHEN table_C.colphi IS NULL THEN table_A.col2 ELSE '' END + '|' +
CASE WHEN table_D.cola IS NULL THEN table_A.col3 ELSE '' END AS Offending_Values
FROM table_A
LEFT JOIN table_B
ON
table_A.col1 = table_B.colx
LEFT JOIN table_C
ON
table_A.col2 = table_C.colphi
LEFT JOIN table_D
ON
table_A.col3 = table_D.cola
WHERE
table_B.colx IS NULL
OR
table_C.colphi IS NULL
OR
table_D.cola IS NULL

My issue is that I need to screen out NULL in the columns of table_A. For example, suppose table_A.col1 is null for a handful of rows. These handful of rows will return (because of the LEFT JOIN). But, because they're NULL, then they didn't really fail in the lookup against table_B. So, I don't want them to return. Normally, in the WHERE clause, I would just put something like

table_A.col1 IS NOT NULL
AND
table_B.colx IS NULL

In my actual situation, is it equivalent to just put this in a for each test? For example, does the following achieve this?

WHERE
(table_B.colx IS NULL AND table_A.col1 IS NOT NULL)
OR
(table_C.colphi IS NULL AND table_A.col2 IS NOT NULL)
OR
(table_D.cola IS NULL AND table_A.col3 IS NOT NULL)

My concern is that I don't want to limit table_A to only those rows where all 3 columns are populated. After all, even with just one of the columns populated, I need to still perform the test.

Or, is this a good way to screen out NULLs at all?

0 个答案:

没有答案