Home > SQL

Delete duplicate rows from table in SQL

29. November 2008

Suppose there is a table called "EmployeeTable" which have some duplicate records.
There is a three way to delete the duplicate rows.

First way to delete duplicate rows :


Select distinct * into Emp_Temp_Table from EmployeeTable

In the above line we are inserting all the distinct row of the "EmployeeTable" to another table "Emp_Temp_Table" (Emp_Temp_Table will create automatically when you use the above query.)
Actuall the above query create clone of EmployeeTable and insert all the distinct row inside the Clone Table (Emp_Temp_Table).


drop table EmployeeTable

sp_rename 'Emp_Temp_Table',EmployeeTable'

Then Delete the orginal table and rename the clone table with the name of orginal table.


Second way to delete duplicate rows :

Select distinct * into Emp_Temp_Table from EmployeeTable

Truncate table EmployeeTable

insert into EmployeeTable select * from Emp_Temp_Table

drop table Emp_Temp_Table


Third way to delete duplicate rows :

Populate the new Primary Key


Alter table EmployeeTable add NewPK int NULL
Go
Declare @intCounter int
Set @intCounter = 0
Update EmployeeTable
SET @intCounter = NewPK = @intCounter + 1

Select name,RecCount=count(*), PktoKeep = max(NewPK)
Into #dupes
From EmployeeTable
Group by name
Having count(*) > 1
Order by count(*) desc


Delete dupes except one Primary key for each dup record


Delete test
from EmployeeTable a join #dupes d
a.name
where a.NewPK not in (select PKtoKeep from #dupes)



Remove the NewPK column


ALTER TABLE test DROP COLUMN NewPK
go

drop table #dupes

Technorati : , ,
Del.icio.us : , ,
Zooomr : , ,
Flickr : , ,

SQL , ,

Comments

3/29/2010 7:34:40 PM #
Interesting blog. It would be great if you can provide more details about it. Thanks a load!
3/29/2010 10:48:30 PM #
Interesting blog. It would be great if you can provide more details about it. Thanks a load!
4/28/2010 5:32:51 AM #
if i upgrade my blog, is it going to keep my same settings?
6/3/2010 8:38:38 AM #
For what its worth, the layout is definitely amazing.  You know how to balance writing and images/videos.  However, I cant get over how little you actually bring to light here.  I think that everyones said the same thing that youve said over and over again.  Dont you think its time for something more?
6/12/2010 3:53:59 AM #
This year the World Cup would be very intense!  It comes down to 50/50 chance with my opinion but good luck to both soccer teams! I should be watching for sure!
6/14/2010 12:04:41 AM #
The World Cup should be very close!   I give it a 50/50 chance with my opinion but good luck to both soccer teams! I should be watching for sure!

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading