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


deleting duplicate records from a table


deleting duplicate records from a table

Author
Message
t.mounika01
t.mounika01
Valued Member
Valued Member (69 reputation)Valued Member (69 reputation)Valued Member (69 reputation)Valued Member (69 reputation)Valued Member (69 reputation)Valued Member (69 reputation)Valued Member (69 reputation)Valued Member (69 reputation)

Group: General Forum Members
Points: 69 Visits: 51
I have created a table Emp_Salary
Create Table Employee_Salary
(
EmployeeID Int,
FirstName Varchar(20),
Salary Money
)

and inserted some records into the table. Some of them are duplicate records. Now, I want to remove all the duplicate records present in the table.The query I wrote was:

Delete from Employee_Salary
WHERE EXISTS (select FirstName,COUNT(FirstName) from Employee_Salary
GROUP BY FirstName
HAving COUNT(FirstName) > 1)

But this is deleting all the records from the table..How can we just delete the duplicate records only?
Dana
Dana
SSCertifiable
SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)

Group: General Forum Members
Points: 5816 Visits: 3450
Try http://www.sqlservercentral.com/articles/T-SQL/dedupingdatainsqlserver2005/2260/
SQLRNNR
SQLRNNR
SSC Guru
SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)

Group: General Forum Members
Points: 65199 Visits: 18570
You can try this article that shows how to do that.

http://jasonbrimhall.info/?s=duplicate+cte



Jason AKA CirqueDeSQLeil
I have given a name to my pain...
MCM SQL Server, MVP


SQL RNNR

Posting Performance Based Questions - Gail Shaw

Luis Cazares
Luis Cazares
SSC-Forever
SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)

Group: General Forum Members
Points: 40964 Visits: 19815

If you try this, I'd suggest to use one of the alternative methods posted in the discussion of the article.


Luis C.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
MMartin1
MMartin1
SSCertifiable
SSCertifiable (7.1K reputation)SSCertifiable (7.1K reputation)SSCertifiable (7.1K reputation)SSCertifiable (7.1K reputation)SSCertifiable (7.1K reputation)SSCertifiable (7.1K reputation)SSCertifiable (7.1K reputation)SSCertifiable (7.1K reputation)

Group: General Forum Members
Points: 7051 Visits: 2033
Something like this ?


WITH myCTE
AS (
SELECT row_number() over (partition by EmployeeID, Firstname,Salary
ORDER BY EmployeeID, FirstName, Salart ) as Colm1,*
from Employee_salary
)
DELETE From myCTE where Colm1> 1



----------------------------------------------------
How to post forum questions to get the best help
sqldriver
sqldriver
SSCrazy
SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)

Group: General Forum Members
Points: 2128 Visits: 2536
I ran into this method in an Itzik Ben-Gan article, which looks more complicated, but of course somehow manages to run a bit faster where I've tested it against a single CTE with ROW_NUMBER() involved, I guess because there's no ORDER BY used anywhere?



;WITH dupe1
AS ( SELECT ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) AS RN ,
[FirstName]
FROM dbo.Emp_Salary
),
dupe2
AS ( SELECT [FirstName] ,
MAX(RN) AS [MAX]
FROM dupe1
GROUP BY [FirstName]
HAVING COUNT(*) > 1
)
SELECT d1.*
--DELETE d1
FROM dupe1 d1
INNER JOIN dupe2 d2
ON d1.[FirstName] = d2.[FirstName]
AND d1.RN < d2.[MAX];


MMartin1
MMartin1
SSCertifiable
SSCertifiable (7.1K reputation)SSCertifiable (7.1K reputation)SSCertifiable (7.1K reputation)SSCertifiable (7.1K reputation)SSCertifiable (7.1K reputation)SSCertifiable (7.1K reputation)SSCertifiable (7.1K reputation)SSCertifiable (7.1K reputation)

Group: General Forum Members
Points: 7051 Visits: 2033
sqldriver (8/27/2014)
I ran into this method in an Itzik Ben-Gan article, which looks more complicated, but of course somehow manages to run a bit faster where I've tested it against a single CTE with ROW_NUMBER() involved, I guess because there's no ORDER BY used anywhere?



;WITH dupe1
AS ( SELECT ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) AS RN ,
[FirstName]
FROM dbo.Emp_Salary
),
dupe2
AS ( SELECT [FirstName] ,
MAX(RN) AS [MAX]
FROM dupe1
GROUP BY [FirstName]
HAVING COUNT(*) > 1
)
SELECT d1.*
--DELETE d1
FROM dupe1 d1
INNER JOIN dupe2 d2
ON d1.[FirstName] = d2.[FirstName]
AND d1.RN < d2.[MAX];



Another consideration on the performance is that this only compares the first name, which multiple people can of course share. The OP did not respond to my suggestion on if what I posted was a good and simple solution. It seems to me that if there is a employeeID in there and no trust issues with the data, then all you have to do is

WITH myCTE
AS (
SELECT row_number() over (partition by EmployeeID
ORDER BY EmployeeID) as Colm1,*
from Employee_salary
)
DELETE From myCTE where Colm1> 1


But since this will very likely be a one time thing (else there is a bigger problem with the system ) , performance will not be as high on the must have list.

----------------------------------------------------
How to post forum questions to get the best help
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