Link to home
Start Free TrialLog in
Avatar of Greens8301
Greens8301

asked on

how to delete duplicate rows in table sql server 2005

I have 6 identical rows of 4 columns table in sql server 2005
I like to have a script to delete the duplicate rows
Is there a way to list rowid ( oracle concept) along with the columns to delete one of the duplicate rows

Thanks
ASKER CERTIFIED SOLUTION
Avatar of reb73
reb73
Flag of Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You can use a Row Number or a RANK function:
http://msdn.microsoft.com/en-us/library/ms186734(SQL.90).aspx
http://msdn.microsoft.com/en-us/library/ms186734(SQL.90).aspx

if you RANK them just in the "partition by" field list ALL the fields then delete where Rank is <> '1'

Similar for Row
Delete  from MyTable where ID Not IN
(
     select  max(ID) from MyTable Group By Duplicate Colum1, Duplicate Column2,Duplicate Column 3
)
Table must have identity column which would identify the duplicate records. Here in MyTable ID as identity column  and duplicate columns are duplicate columns 1,2 & 3 (very obvious to know...!!!)
Also refer :   http://support.microsoft.com/kb/139444