Thursday 26 July 2012

To delete the duplicate records


To delete the duplicate records

1. DELETE
FROM    MyTable
WHERE   ID NOT IN
        (SELECT        MAX(ID)
        FROM           MyTable
        GROUP BY       DuplicatevalueColumn1, DuplicateValueColumn2, DuplicateValueColumn3)

2. DELETE f
FROM (
SELECT ROW_NUMBER() OVER (PARTITION BY PHONE_NBR order by PHONE_NBR) AS RecID
FROM Table_2
) AS f
WHERE RecID > 1

Here PHONE_NBR is the suspected duplicate column

3. Set rowcount 1

Delete table_2 from table_2 A
Where (Select count(*) from table_2 B where B.PHONE_NBR= A.PHONE_NBR)>1

While @@rowcount > 0
Delete table_2 from table_2 A Where (Select count(*) from table_2 B where B.PHONE_NBR= A.PHONE_NBR)> 1

Set rowcount 0

No comments:

Post a Comment