Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

Deleting Duplicate Records Expand / Collapse
Author
Message
Posted Friday, December 19, 2008 10:59 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, December 31, 2008 5:22 AM
Points: 0, Visits: 5
Comments posted to this topic are about the item Deleting Duplicate Records
Post #623201
Posted Wednesday, December 24, 2008 6:23 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Monday, April 07, 2014 8:19 AM
Points: 48, Visits: 195
Why not just do this?

Insert into EmployeeCopy
select * from Employee
UNION
select * from Employee

voila -- duplicates gone! However, they have to be duplicates in every field for this to work, but I suppose they're not really true duplicates if they don't match in every field.

Post #625329
Posted Wednesday, December 24, 2008 7:15 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: 2 days ago @ 7:05 AM
Points: 15, Visits: 180
In the case where there's no unique ID, I would think another option would be

select distinct * from Employee
into EmployeeCopy

Since I'm typing this before I've had coffee, it could be that you need to replace "*" with a list of all the fields.
Post #625358
Posted Wednesday, December 24, 2008 7:20 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, April 10, 2012 1:22 PM
Points: 5, Visits: 19
delete * from employee where id NOT IN (select min(id) from employee group by employeeno,employeeid)

This will delete all duplicated records and leave one unduplicated record

RickO
Post #625365
Posted Wednesday, December 24, 2008 7:52 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Monday, April 07, 2014 8:19 AM
Points: 48, Visits: 195
Now that I read the article again, I come to these conclusions:
EmployeeNo and EmployeeID are duplicate fields in the Employees table
ID field only exists in the EmployeesCopy table because that was added to make the rows non unique (but somehow it's shown in the Employees table, so I'm a little confused)
I would hope that if there are any other fields in the table, they would match exactly too -- or you've got bigger problems and you shouldn't be doing this to get rid of them

So using the select distinct into EmployeesCopy or select * UNION select * will remove the duplicates as they fill EmployeeCopy -- thus eliminating the need to look at ID > ID or MIN(ID) with a self join.

However, if you choose to use the author's solution and you don't remove the duplicates before filling EmployeesCopy, isn't the delete EmployeesCopy where not in (select min(id)) an example of RBAR?
Would it be better to do it this way?
delete EmployeesCopy from EmployeesCopy a left join (select min(id) from EmployeesCopy group by EmployeeNo, EmployeeID) on a.id = b.id where b.id is null
This uses the set-based power of SQL, so shouldn't it be faster?
Post #625390
Posted Wednesday, December 24, 2008 8:11 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, April 10, 2012 1:22 PM
Points: 5, Visits: 19
I think the author meant to say that the ID column is unique

his query is:

--Find duplicate records
--The result of this query is all the duplicate records whose Id is greater.
select a.* from Employees a
join Employees b on
a.[EmployeeNo] = b.[EmployeeNo]
AND a.[EmployeeID]= b.[EmployeeID]
AND a.Id>b.Id

so to delete records without creating a new table
delete * from employee where id NOT IN (select min(id) from employee group by employeeno, employeeid)

This will leave the min(id) for any duplicated record, correct?
Post #625405
Posted Wednesday, December 24, 2008 8:18 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Monday, April 07, 2014 8:19 AM
Points: 48, Visits: 195
OK.

Still, wouldn't it be better to do this (set-based solution):
delete employees from employees a left join (select min(id) from employees group by employeeno, employeeid) b on a.id = b.id where b.id is null

rather than this (RBAR solution):
delete employees where id NOT IN (select min(id) from employees group by employeeno, employeeid)

On small tables there probably wouldn't be much difference in performance, but I'm thinking that the set-based solution would be orders of magnitude faster on very large tables.
Post #625407
Posted Wednesday, December 24, 2008 8:26 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, April 10, 2012 1:22 PM
Points: 5, Visits: 19
What is RBAR?

I agree performance may be better....but probably in most cases
it wouldn't matter on tables with less than 20,000 records

Post #625412
Posted Wednesday, December 24, 2008 8:28 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Monday, April 07, 2014 8:19 AM
Points: 48, Visits: 195
Row By Agonizing Row -- I'm surprised that you've never heard of it!

For each row in Employees you have to run the query (select min(id) from Employees group by employeeid, employeeno)

The set-based solution does this just one time and joins back to Employees to perform the deletes.

You are right, you probably wouldn't notice any difference in performance on small tables, but I'd bet 20,000 rows would show a noticeable difference, and the difference in 1,000,000 plus row tables could be counted in minutes, maybe hours.
Post #625415
Posted Wednesday, December 24, 2008 9:31 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, April 10, 2012 1:22 PM
Points: 5, Visits: 19
RBAR...didn't know that...!

I did explain the plan for both and believe it or not the
delete where NOT IN is less cost

Is cost the indicator to look at for the execution plan?
Post #625453
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse