SQL - find duplicates records, but delete the oldest

zoe Ohara 286 Reputation points
2021-04-27T13:08:51.607+00:00

Hi!

I've been struggling with a query all day, just cant seem to find the logic..

I have a table:

CREATE TABLE [dbo].[mytable](
 [Id] [int],
 [id2] [int],
 [id3] [int],
 [id4] [int],
 [CreatedOn] [datetime] 

I have to find records where id2, id3 & id4 are the same and then delete the one with the lowest CreatedOn value. (Id is the primary key)

Any ideas how to achieve this?

Thanks,

Zoe

Developer technologies Transact-SQL
0 comments No comments
{count} votes

Accepted answer
  1. Guoxiong 8,206 Reputation points
    2021-04-27T13:17:50.957+00:00

    Try this:

    ;WITH CTE_Duplicates AS (
        SELECT [Id], [Id2], [Id3], [Id4], ROW_NUMBER() OVER(PARTITION BY [Id2], [Id3], [Id4] ORDER BY [CreatedOn] DESC) AS rn
        FROM [dbo].[mytable]
    )
    
    DELETE CTE_Duplicates WHERE rn > 1;
    
    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Tom Cooper 8,481 Reputation points
    2021-04-27T14:12:15.903+00:00

    @Guoxiong 's answer will delete all but the newest row and leave only one row of each duplicate. If you want to just delete the oldest row, then you want

     ;WITH CTE_Duplicates AS (  
         SELECT [Id], [Id2], [Id3], [Id4], ROW_NUMBER() OVER(PARTITION BY [Id2], [Id3], [Id4] ORDER BY [CreatedOn] ASC) AS rn  
         FROM [dbo].[mytable]  
     )  
       
    DELETE CTE_Duplicates WHERE rn = 1;  
    

    Tom

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.