SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


delete duplicates


delete duplicates

Author
Message
wschampheleer
wschampheleer
Ten Centuries
Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)

Group: General Forum Members
Points: 1032 Visits: 303
Because you say your table is big and you don't seem to have the right indexes, you can use the following as an alternative to doing a select distinct or a row_number() over():

Create a new table with a unique index on the colums where you don't want duplicates but use the IGNORE_DUP_KEY = ON option (see http://msdn.microsoft.com/en-us/library/ms188388.aspx). Copy all records from the old table to the new one, drop the old table and rename the new one.

Done.

Regards,

Willem
http://wschampheleer.wordpress.com
Jimit Shah
Jimit Shah
Forum Newbie
Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)

Group: General Forum Members
Points: 3 Visits: 3
I have found good article on deleting duplicate record. You can see T-SQL Query To Delete Duplicate Records Among Identical Rows In A Table....
Smash125
Smash125
SSC Veteran
SSC Veteran (283 reputation)SSC Veteran (283 reputation)SSC Veteran (283 reputation)SSC Veteran (283 reputation)SSC Veteran (283 reputation)SSC Veteran (283 reputation)SSC Veteran (283 reputation)SSC Veteran (283 reputation)

Group: General Forum Members
Points: 283 Visits: 1381
Can be done using temp table

SELECT EmpID,EmpName INTO #temp
FROM Employees
GROUP BY EmpID,EmpName
HAVING COUNT(*)>1

DELETE Employees FROM Employees a INNER JOIN #temp b ON
a.EmpID=b.EmpID

INSERT INTO Employees(EmpID,EmpName)
SELECT EmpID,EmpName FROM #temp
Smash125
Smash125
SSC Veteran
SSC Veteran (283 reputation)SSC Veteran (283 reputation)SSC Veteran (283 reputation)SSC Veteran (283 reputation)SSC Veteran (283 reputation)SSC Veteran (283 reputation)SSC Veteran (283 reputation)SSC Veteran (283 reputation)

Group: General Forum Members
Points: 283 Visits: 1381
Can be done using temp table for your reference

SELECT EmpID,EmpName INTO #temp
FROM Employees
GROUP BY EmpID,EmpName
HAVING COUNT(*)>1

DELETE Employees FROM Employees a INNER JOIN #temp b ON
a.EmpID=b.EmpID

INSERT INTO Employees(EmpID,EmpName)
SELECT EmpID,EmpName FROM #temp
tfifield
tfifield
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1297 Visits: 2890
A simple way to delete dups that works on versions earlier than 2005 is:


DELETE EMP
FROM Employees EMP
INNER JOIN
(SELECT MIN(EmpID) AS EmpID, EmpName
FROM Employees
GROUP BY EmpName
HAVING COUNT(*) > 1
) AS DUP ON
EMP.EmpName = DUP.EmpName
AND EMP.EmpID <> DUP.EmpID


Todd Fifield
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search