How to Find Rows which are Duplicates by a Key but Not Duplicates in All Columns?

后端 未结 5 2061
逝去的感伤
逝去的感伤 2020-12-30 05:31

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.

5条回答
  •  無奈伤痛
    2020-12-30 06:08

    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....

提交回复
热议问题