Showing posts with label delete duplicate rows from table. Show all posts
Showing posts with label delete duplicate rows from table. Show all posts

Wednesday, 16 July 2014

Delete Duplicate Rows in SQL Server.


Method 1:
DELETE FROM table_name A WHERE ROWID > (
SELECT min(rowid) FROM table_name B
WHERE A.key_values = B.key_values);


Method 2:
create table table_name2 as select distinct * from table_name1;
drop table table_name1;
rename table_name2 to table_name1;

Method 3:

WITH CTE (COl1,Col2, DuplicateCount)
AS
(
SELECT COl1,Col2,
ROW_NUMBER() OVER(PARTITION BY COl1,Col2 ORDER BY Col1) AS DuplicateCount
FROM DuplicateRcordTable
)
DELETE
FROM CTE
WHERE DuplicateCount > 1
GO