0

I am in the midst of uploading and updating my db from data from a third party source. Unfortunately, there are many duplicate records in the data from the third party data source.

I looked at a few questions here on SO but all of them seem to be cases where there is an ID column which differentiates one row from the other.

In my case, there is no ID column. e.g.

State   City    SubDiv  Pincode Locality Lat    Long
Orissa  Koraput Jeypore 764001  B.D.Pur 18.7743 82.5693
Orissa  Koraput Jeypore 764001  Jeypore 18.7743 82.5693
Orissa  Koraput Jeypore 764001  Jeypore 18.7743 82.5693
Orissa  Koraput Jeypore 764001  Jeypore 18.7743 82.5693
Orissa  Koraput Jeypore 764001  Jeypore 18.7743 82.5693

Is there a simple query which I can run to delete all duplicate records and keep one record as the original? So in the above case I want to delete rows 3,4,5 from the table.

I am not sure if this can be done using simple sql statements but would like to know others opinion how this can be done

1
  • 4
    Could you not just add an ID column to your table, then use one of the methods you've already read about? Also, it may be worth looking into not importing the duplicates from the other data source, if you don't want them in your table. Commented Sep 22, 2011 at 11:41

5 Answers 5

7
;with cte as(
select State City, SubDiv, Pincode, Locality, Lat, Long, 
row_number() over (partition by City, SubDiv, Pincode, Locality, Lat,Long order by City) rn
from yourtable
)
delete cte where rn > 1
Sign up to request clarification or add additional context in comments.

Comments

5

I would insert the third party data to a temporary table that then:

insert into
  target_table
select distinct
  *
from
  temporary_table

and finally delete the temporary table.

Only distinct (unique) rows will be inserted to the target table.

2 Comments

+1 Another approach is to copy the data into the temp table with DISTINCT. I like this approach better because it gives you a chance to validate the result of the operation; delete cte where rn > 1 should also work but if you make a mistake, you already destroyed data.
Just wanted to add, that this solution works just fine (as does the solution proposed by @t-clausen.dk) but does nothing to prevent this from happening again. After dedupping you need to put a unique index on the natural key of your data. You may also need to fix your import process.
3

One of

  • add a column to de-duplicate and leave it
  • do a SELECT DISTINCT * INTO ANewTable FROM OldTable and then rename etc
  • Use t-clausen.dk's CTE approach

And then add a unique index on the desired columns

Comments

2

You may use the ROW_NUMBER() function : SQL SERVER – 2005 – 2008 – Delete Duplicate Rows

Comments

0

Try this

alter table mytable add id int identity(1,1)

delete  mytable  where id in (
select duplicateid from (select ROW_NUMBER() over (partition by State ,City ,SubDiv ,Pincode ,Locality ,Lat ,Long order by State ,City ,SubDiv ,Pincode ,Locality ,Lat ,Long ) duplicateid
from mytable) t where duplicateid !=1)

alter table mytable drop column id 

2 Comments

id in (select *? What value does the identity column add? This is close but see @t-clausen.dk's answer - no identity column needed.
If you post code, XML or data samples, please highlight those lines in the text editor and click on the "code samples" button ( { } ) on the editor toolbar to nicely format and syntax highlight it!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.