Single Query to delete and display duplicate records

前端 未结 3 954
感情败类
感情败类 2021-02-10 04:35

One of the question asked in an interview was,

One table has 100 records. 50 of them are duplicates. Is it possible with a single query to delete the

3条回答
  •  迷失自我
    2021-02-10 04:50

    with SQL Server you would use something like this

    DECLARE @Table TABLE (ID INTEGER, PossibleDuplicate INTEGER)
    
    INSERT INTO @Table VALUES (1, 100)
    INSERT INTO @Table VALUES (2, 100)
    INSERT INTO @Table VALUES (3, 200)
    INSERT INTO @Table VALUES (4, 200)
    
    DELETE FROM @Table
    OUTPUT Deleted.*
    FROM  @Table t
          INNER JOIN (
            SELECT    ID = MAX(ID)
            FROM      @Table
            GROUP BY  PossibleDuplicate
            HAVING    COUNT(*) > 1
          ) d ON d.ID = t.ID
    

    The OUTPUT statement shows the records that get deleted.

    Update:

    Above query will delete duplicates and give you the rows that are deleted, not the rows that remain. If that is important to you (all in all, the remaining 50 rows should be identical to the 50 deleted rows), you could use SQL Server's 2008 MERGE syntax to achieve this.

提交回复
热议问题