开发者

Single Query to delete and display duplicate records

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 duplicate records from the table as well as select and开发者_运维知识库 display the remaining 50 records.

Is this possible in a single SQL query?

Thanks

SNA


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.


Lieven's Answer is a good explanation of how to output the deleted rows. I'd like to add two things:

  1. If you want to do something more with the output other than displaying it, you can specify OUTPUT INTO @Tbl (where @Tbl is a table-var you declare before the deleted);

  2. Using MAX, MIN, or any of the other aggregates can only handle one duplicate row per group. If it's possible for you to have many duplicates, the following SQL Server 2005+ code will help do that:

 

;WITH Duplicates AS
(
    SELECT
        ID,
        ROW_NUMBER() OVER (PARTITION BY DupeColumn ORDER BY ID) AS RowNum
)
DELETE FROM MyTable
OUTPUT deleted.*
WHERE ID IN
(
    SELECT ID
    FROM Duplicates
    WHERE RowNum > 1
)


Sounds unlikely, at least in ANSI SQL, since a delete only returns the count of the number of deleted rows.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜