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
 General SQL Server Forums
 New to SQL Server Programming
 delete duplicates (not exact) by 2 columns

Author  Topic 

arielspecial
Starting Member

11 Posts

Posted - 2010-06-29 : 04:09:48
Hi, I have tried to create this script, but got stuck:
for each userID in a table(there are multiple times the same userID) I want to get all the records where for this userID delete all records with the same dateInserted column except one. if I have 5 times the same userID and same date delete only 4 records.

pk_bohra
Master Smack Fu Yak Hacker

1182 Posts

Posted - 2010-06-29 : 04:30:15
Hi,

The following link should help you:
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=6256

Regards,
Bohra

I am here to learn from Masters and help new bees in learning.
Go to Top of Page

vaibhavktiwari83
Aged Yak Warrior

843 Posts

Posted - 2010-06-29 : 05:04:20
If that is confusing for you to get the solution quickly then
Try this -
But the topic may give you more information than your need...

;WITH CTE AS
(
SELECT *, ROW_NUMBER() OVER ( Partition By UserID, Date ORDER BY UserID ) Seq FROM TableName
)

DELETE FROM CTE WHERE Seq <> 1


Vaibhav T

To walk FAST walk ALONE
To walk FAR walk TOGETHER
Go to Top of Page
   

- Advertisement -