Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
sinhakg
Starting Member
1 Post |
Posted - 2009-08-18 : 11:54:17
|
Hi, I saw many solution for deleting duplicate rows in many articles published on various website.Even in the forum of msdn [url]http://support.microsoft.com/kb/139444[/url]Every solution anyhow delete the existing table in which the duplicate rows are found.(Either copy or murge the table to new one.Now I will provide one better solution to delete the table's duplicate row without deleting table.Simplest way to delete uplicate records from table having Primary KeyLet a table tblA with column X, Y and Z has some duplicate rows vizX Y Z1 abc def2 abc fgh3 cdf fgh4 cdf fghHere I consider X is as Primary Key.Now below is the solution for deleting the duplicate rowspossessed by column Y.delete from tblAwhere X In(select a.X from tblA a inner join tblA b on a.X<> b.X and a.Y= b.Y)and X not in(select min(a.X) from tblA a inner join tblA b on a.X<> b.X and a.Y= b.Ygroup by a.Y)This is the simplest & optimised solution to delete duplicate records from table having primary key.and finally you will get the result like below:X Y Z1 abc def3 cdf fgh Simplest way to delete duplicate records from table having No Primary key e.g. below table (tblA)Y Zabc defabc fghcdf fghcdf fgh-> First create a tempoaraty table with all column with respective datatype (@tblTempA)-> select the column for which you have to delete the records (e.g. I choose Y)insert @tblTempAselect distinct(Y),Z from tblAdelete tblAinsert tblAselect * from @tblTempAand finally you will get the result like below:Y Zabc defcdf fghWrite me for help at My MailAlways be optimised |
|
|
|
|