|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Wednesday, December 31, 2008 5:22 AM
Points: 0,
Visits: 5
|
|
|
|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: Thursday, January 17, 2013 5:28 AM
Points: 42,
Visits: 170
|
|
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.
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Wednesday, May 08, 2013 11:26 AM
Points: 12,
Visits: 158
|
|
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.
|
|
|
|
|
Forum 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
|
|
|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: Thursday, January 17, 2013 5:28 AM
Points: 42,
Visits: 170
|
|
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?
|
|
|
|
|
Forum 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?
|
|
|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: Thursday, January 17, 2013 5:28 AM
Points: 42,
Visits: 170
|
|
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.
|
|
|
|
|
Forum 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
|
|
|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: Thursday, January 17, 2013 5:28 AM
Points: 42,
Visits: 170
|
|
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.
|
|
|
|
|
Forum 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?
|
|
|
|