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 2005 Forums
 Transact-SQL (2005)
 delete duplicate records

Author  Topic 

esthera
Master Smack Fu Yak Hacker

1410 Posts

Posted - 2007-04-27 : 03:53:27
i have a table that's data is a bit messed up and it has many duplicate records.

Is there any easy way I can loop through and delete all duplicate records except the latest one)
i only want to do this if all the fields are exactly the same except the id field (this is identity so it is different on all the fields)

please advise?

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-04-27 : 04:32:53
DELETE t
FROM (SELECT ROW_NUMBER() OVER (PARTITION BY <Col list here to identify duplicates> ORDER BY <col list here for which record to save>) AS t
WHERE RecID = 1


Peter Larsson
Helsingborg, Sweden
Go to Top of Page
   

- Advertisement -