Monday, March 12, 2012

Deleting duplicate rows on table with/without primary key in Sql Server


Table with primary key
Id    name
1                     Harpreet
2                     Harpreet
3                     Harpreet
4                     John
5                     Smit
select min(id) from #tablewithprimary group name
=1,4,5
Delete from .. where id not in (1,4,5)
Result : 1,4,5
delete from #tablewithprimary where id not in   
  (select min(id) from #tablewithprimary group by course,subject) 

No comments:

Post a Comment