Solved

Delete duplicate records in SQL database using stored procedure

Posted on 2007-12-04
12
1,711 Views
Last Modified: 2008-09-11
I have a SQL server 2000 database that has over a million records in it.  I need to create a stored procedure that i can run that we check for which records have duplicates and then delete all of the duplicates except for one.  The criteria for the duplicates is based on five different columns.  I am hoping somebody can give me some guideance on how to do this.  
0
Comment
Question by:Mike_Stevens
12 Comments
 
LVL 17

Expert Comment

by:Daniel Reynolds
ID: 20405242
Can you provide the structure of the table? Is there a unique key?
0
 

Author Comment

by:Mike_Stevens
ID: 20405276

tblStatus
Column: RecordNumber -  Unique Key
Column: IDNum
Column: Code
Column: Date
Column: Time
Column: State

All of the columns except RecordNumber need to be used in criteria.  Sample below

RecordNumber  IDNum  Code  Date         Time   State
------------------  ---------  ------  -------        ------   --------
100                   12345     A     12122007  1630    CA
101                   12345     A     12122007  1630    CA
102                   12345     A     12122007  1630    CA
103                   12345     A     12122007  1630    CA
104                   12345     A     12122007  1630    CA

Using the sample data above, procedure needs to delete all but one of the records.

0
 
LVL 3

Expert Comment

by:randomjames
ID: 20405356
I would create a new table with the unique index on those fields, export the data from your old table into this new one (which will only take one of each duplicate because of the index), then clear out your old table and move the records from your new table back to the old table.
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 17

Accepted Solution

by:
Daniel Reynolds earned 500 total points
ID: 20405880
Here is some code you can run iteratively until the rows affected = 0

SET ROWCOUNT 1
DELETE  dbo.tblStatus
--SELECT top 1 * 
FROM dbo.tblStatus
WHERE Exists (Select Count(*), IDNum, Code, [Date], [Time], [State]
                              from dbo.tblStatus
                             Group By IDNum, Code, [Date], [Time], [State]
                             Having Count(*) > 1)

Open in new window

0
 
LVL 17

Expert Comment

by:Daniel Reynolds
ID: 20405903
And here is a link with additional information.
http://support.microsoft.com/kb/139444
and one that shows how to do it with a little bit more programming, but all at once.
http://www.sql-server-performance.com/articles/dev/dv_delete_duplicates_p1.aspx

0
 
LVL 11

Expert Comment

by:yuching
ID: 20408127
Code provided by xDJR1875: will delete all record which have duplicate data but Mike_Stevens intended to leave a row for the duplicate data.

You can try as below, it will only left the max record number for the duplicates data.

DELETE  FROM tblStatus
WHERE  NOT EXISTS (
        SELECT MaxRecordNumber, IDNum, Code, Date, Time, State
        FROM
        (
              SELECT Max(RecordNumber) AS MaxRecordNumber, IDNum, Code, Date, Time, State
              FROM  dbo.tblStatus
              GROUP BY IDNum, Code, Date, Time, State
          ) A
         WHERE A.MaxRecordNumber = tblStatus.RecordNumber AND
                     A.IDNum = tblStatus.IDNum AND A.Code = tblStatus.Code AND
                     A.Date = tblStatus.Date AND A.Time = tblStatus.Time AND
                     A.State = tblStatus.State
)
0
 
LVL 17

Expert Comment

by:Daniel Reynolds
ID: 20408152
Actually, with the SET ROWCOUNT = 1
The code does not delete all duplicate records.
0
 
LVL 11

Expert Comment

by:yuching
ID: 20408626
Sorry, my mistake, but for the example of data, the duplicate rows can be more than 2, set rowcount =1  will only delete one of the records which having duplicate.
0
 
LVL 17

Expert Comment

by:Daniel Reynolds
ID: 20409336
You are correct that is why I suggest Mike run it iteratively until rows affected = 0.

It is not the most elegant of solutions, but maybe one of  the simplest.
0
 

Expert Comment

by:yordan_georgiev
ID: 21502162
DELETE
    FROM    MyTable
    WHERE    ID NOT IN
    (
    SELECT    MAX(ID)
        FROM        MyTable
        GROUP BY    DuplicatevalueColumn1, DuplicateValueColumn2,
                DuplicateValueColumn2)

Reference:Pinal Dave
http://blog.sqlauthority.com/2007/03/01/sql-server-delete-duplicate-records-rows/
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question