I am working with a table which is an extract of a set of other tables. All of the rows of the extract table should be unique according to keys D1, D2 and D3. They are not.
This would have performance limitations, but is much easier to understand:
SELECT D1, D2, D3
FROM TEST_KEY TK
WHERE (D1, D2, D3) IN
(SELECT D1, D2, D3 FROM TEST_KEY TK2
GROUP BY D1, D2, D3
HAVING COUNT(*) > 1)
AND (D1, D2, D3) IN
(SELECT D1, D2, D3 FROM TEST_KEY TK2
GROUP BY D1, D2, D3, C4, C5, C6
HAVING COUNT(*) < 2)
Unable to test on SQL-Server, hope the syntax is good.
Again, not sure if you have analytic functions in SQL-Server, but this one works in Oracle and might be faster:
WITH BAD_DUP AS (
SELECT TK.*,
COUNT(1) OVER (PARTITION BY D1, D2, D3, C4, C5, C6 ORDER BY D1) FULL_DUP,
COUNT(1) OVER (PARTITION BY D1, D2, D3 ORDER BY D1) KEY_DUP
FROM TEST_KEY TK)
SELECT * FROM BAD_DUP
WHERE FULL_DUP < KEY_DUP
Would like to get it down to a single query....