Discussion Locked

Delete Duplicate

0 helpful
8 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.
How do i delete duplicate records in SQL Server 7.0. What is SQL codes or can i just filter them out.

8 replies

by mark.thomson

I do'nt know if there is a specific T-SQL way to delete duplicate records but try this -

1) Create a view which identifies the duplicate records

for a table with rows in the form
Pkey,f1,f2,f3

this would be something like

SELECT f1,f2,f3 FROM Table1 GROUP BY f1,f2,f3 HAVING (COUNT(f1) > 1)

2) To Delete _1_ of the duplicates

DELETE FROM Table1 WHERE PKey = (SELECT MAX(Pkey) FROM TABLE1 INNER JOIN vw_Table1Duplicates ON Table1.f1 = vw_Table1Duplicates.f1 AND Table2.f2 = vw_Table1Duplicates.f2 AND Table3.f3 = vw_Table1Duplicates.f3 GROUP BY vw_Table1duplicates.f1,vw_Table1duplicates.f2,vw_table1duplicates.f3)

(where vw_table1duplicates is your new view)

for each duplicated set of f1,f2 & f3, the number of duplicates will be reduced by 1 every time the above delete query is executed.

I recommend that you try this out on some test data before you use it live and that once you've cleaned out all your duplicates that you use an index to prevent duplicate records if they are a problem

by chandrashekargr

First you got to identify which among the duplicate is required.

Whenever you create records having same values to the key-column SQL server internally creates a unique identifier called record-id/row-id. This row/record identifier is a hidden column that gets created when you create a new table and it stores integer numbers in an incremental fashion. With the help of this column you can delete duplicate records.

If you have 10 duplicate columns, their rowid would be in an ascending order indicating which was created first (row with a min rowid) and which is the latest (row with an max rowid).

If you think that the first among the duplicate is the required record then try the following SQL.

DELETE FROM YOURTABLE WHERE ROWID <> (SELECT MIN(ROWID) FROM YOURTABLE WHERE KEY-COLUMN='SOMEVALUE')

If the required row is not the first or the last one the try the following.

Example:

SELECT ROWID,KEY-COLUMN,..othercolumns FROM YOUR-TABLE WHERE KEY-COLUMN='SOMEVALUE'
will display all records satisfying the condition and here you will notice that ROWID column will have unique values. Identify the record which you want and note down the corresponding rowid value (say 1005)

DELETE FROM YOURTABLE
WHERE KEY-COLUMN='SOMEVALUE' AND
ROWID <> 1005

This will delete all the records execpt the one with rowid 1005.

Hope this example is clear enough.

by chrandrob

Here is the quick and easy solution. First of all, you need to have a unique field on your table. As a general rule of thumb, almost all tables that I create have an identity field as the primary key. For this example, we'll call this unique field ListID and the table MyData. If needed you can add an identity field, delete the duplicates, and then drop the identity column. The field that I will be checking for duplicates will be called AccountNum.

Now, you simply need to (INNER) join the table to itself in a delete clause. I hope you are familiar with aliasing. If not, I'll give you the ten cent explanation. To keep the query easy to understand for me and SQL Server, I am going to give each instance of the MyData table an alias: copy1 and copy2. Like this:

DELETE copy1
FROM MyData copy1
INNER JOIN MyData copy2 ON copy2.AccountNum = copy1.AccountNum
WHERE copy2.ListID > copy1.ListID

The "Where" clause above will cause the oldest entry to be deleted. If you want the newest entry deleted instead, change it to: WHERE copy2.ListID < copy1.ListID

This discussion is closed to new replies.