Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Duplicates

Author  Topic 

Steve2106
Posting Yak Master

183 Posts

Posted - 2012-07-25 : 12:17:35
Hi There,

Can someone help me with the sql code to delete duplicate rows.

I have a field called UID that is not keyed but there should not be duplicates. I have just merged an old database to a new one and accidentaly inserted duplicate records.

I need to delete any rows that have a duplicate based on the field UID.

Thanks for your help.

Best Regards,



Steve

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-07-25 : 12:21:36

whats rule for determining which one of records with same value for UID to be retained? or is it that they're all perfectly identical and you just need to retain a random one?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

Steve2106
Posting Yak Master

183 Posts

Posted - 2012-07-25 : 14:00:35
Hi There,

Thanks for the reply.

The records are identical except the primary key autoid.

Best Regards,



Steve
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-07-25 : 14:05:59
quote:
Originally posted by Steve2106

Hi There,

Thanks for the reply.

The records are identical except the primary key autoid.

Best Regards,



Steve


so which ones to be retained?
one with earliest,latest or random value of pk?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

Steve2106
Posting Yak Master

183 Posts

Posted - 2012-07-31 : 06:11:09
Hi There,

Thanks for the reply.

I'm sorry it has taken a while to answer your question. I have been off ill.

Preferably I would like the earlier ones to stay.

Thanks for your help, as always I appreciate it.

Best Regards,



Steve
Go to Top of Page

LoztInSpace
Aged Yak Warrior

940 Posts

Posted - 2012-07-31 : 06:50:48
If you can determine how to keep one and not another they're not identical are they?
So first you need to work out how identical they really are:

select col1, col2, coln .. all except GUID from table
group by col1, col2, coln
having count(*)>1

Then you'll know what you're up against.

Then if you can just delete any old one you need to
select * from
(
select col1, col2, col3, guid, rownum() over (order by ... partition by col1, col2...) rn
) x
where rn=1
and delete those.

Something like that - There's plenty of examples on this forum if you search a little harder.

Anyway, the reason for posting this:
Remember when chosing a surrogate key, *you also need to create a unique constraint on your actual key*.
Now you've done that and counted the cost in maintaining the same rule twice, think if you really need that surrogate after all.
Go to Top of Page
   

- Advertisement -