12/19/08

SQL SERVER - Delete Duplicate Records - Rows

Following code is useful to delete duplicate records. The table must have identity column, which will be used to identify the duplicate records. Table in example is has ID as Identity Column and Columns which have duplicate data are DuplicateValueColumn1, DuplicateValueColumn2 and DuplicateValueColumn3.

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



EXAMPLE
----------



if there is no key in the table then what ?

I have on table named [Duplicate] {ID int,FNAME varchar(10),MNAME varchar(10)}

Here there is no key and here are duplicate rows. so hoca can i delete this duplicate rows.

Check Data

ID FNAME LNAME
1 AAA CCC
2 BBB DDD
1 AAA CCC
2 BBB DDD
1 AAA CCC
2 BBB DDD
3 BCB DGD

Remove duplicate rows and keep the data in to the table like this using single query.

ID FNAME LNAME
1 AAA CCC
2 BBB DDD
3 BCB DGD

No comments:

Post a Comment

Welcome