Ask a questionAsk a question
 

AnswerDelete Duplicate Records

  • Wednesday, November 04, 2009 3:53 PMSql Hates Me Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi

    I need some help getting a script out to delete duplicate records in my database table.

    Take the following example scenario :

    Table Structure

    Artist VARCHAR(100)
    Album VARHCAR(100)
    Title VARCHAR(100)
    Duration int
    DateAdded DATETIME

    The duplicated fields are the artist, album and title, the DateAdded field of the duplicated records could be out by up to 30 seconds and the Duration can be out by 2 seconds.

    Any help would be appreciated!

    Thanks

Answers

  • Wednesday, November 04, 2009 3:57 PMAbdshall Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code
    Here is one way to keep the most current record and delete the rest(SQL 2005 and up)

    ;WITH CTE AS
    (
    	SELECT Artist,Album,Title,Duration
    	,DateAdded,ROW_NUMBER() OVER(PARTITION BY Artist,Album,Title ORDER BY DateAdded DESC,Duration DESC) AS Row
    	FROM TableStructure
    )
      DELETE FROM CTE WHERE Row > 1
    



    Abdallah, PMP, MCTS

All Replies

  • Wednesday, November 04, 2009 3:57 PMAbdshall Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code
    Here is one way to keep the most current record and delete the rest(SQL 2005 and up)

    ;WITH CTE AS
    (
    	SELECT Artist,Album,Title,Duration
    	,DateAdded,ROW_NUMBER() OVER(PARTITION BY Artist,Album,Title ORDER BY DateAdded DESC,Duration DESC) AS Row
    	FROM TableStructure
    )
      DELETE FROM CTE WHERE Row > 1
    



    Abdallah, PMP, MCTS
  • Wednesday, November 04, 2009 3:59 PMSQLUSAAnswererUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    The simplest DEDUP is SELECT INTO table create, followed by update of the values you want to keep for Duration & Dateadded. Start with renaming the table zzzMusicAlbum (to be deleted in the future).

    SELECT   Artist, 
             Album, 
             Title, 
             Duration = MIN(Duration), 
             DateAdded = MIN(DateAdded) 
    INTO     MusicAlbum 
    FROM     zzzMusicAlbum 
    GROUP BY Artist, 
             Album, 
             Title 
    ORDER BY Artist, 
             Album, 
             Title 
    

    Kalman Toth, SQL Server & BI Training, SSAS, SSIS, SSRS; http://www.SQLUSA.com