Here is a query by which you can delete duplicate rows without using identity column
/* Delete Duplicate records */
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
Showing posts with label duplicate. Show all posts
Showing posts with label duplicate. Show all posts
Monday, November 8, 2010
Wednesday, October 13, 2010
[T-SQL] How to remove duplicate data
One of the very common question that is asked many times , tell the way how can we delete the duplicate data or an easiest way or fastest way.
This method has one limitation that the table should have a primary key.
DELETE
FROM TableName
WHERE ID NOT IN
(
SELECT MAX(ID)
FROM TableName
GROUP BY Col1, Col2, Col3)
*TableName should be replaced with Table Name
*Col1 , Col2 and Col3 must be replaced with the criteria on which records will qualify as duplicate. It can be increase and decrease.
This method has one limitation that the table should have a primary key.
DELETE
FROM TableName
WHERE ID NOT IN
(
SELECT MAX(ID)
FROM TableName
GROUP BY Col1, Col2, Col3)
*TableName should be replaced with Table Name
*Col1 , Col2 and Col3 must be replaced with the criteria on which records will qualify as duplicate. It can be increase and decrease.
Subscribe to:
Posts (Atom)