Wednesday, February 22, 2012

Find and delete duplicate records in sql server

;WITH SalesCTE(Product, SaleDate, SalePrice, Ranking)
AS
(
SELECT
        Product, SaleDate, SalePrice,
Ranking = DENSE_RANK() OVER(PARTITION BY Product, SaleDate, SalePrice ORDER BY NEWID() ASC)
FROM SalesHistory
)
DELETE FROM SalesCTE
WHERE Ranking > 1

No comments:

Post a Comment