11 October 2012

Finding Duplicate Records in SQL and Deleting Duplicate Records


SELECT REQUEST_ID, COUNT(*) AS [DupeCount]
FROM REQUESTDETAILS  where UPLOAD_DATE >= CONVERT(date,getdate()-2,101)
and CLIENTNAME = 'lluh'
GROUP BY REQUEST_ID
HAVING COUNT(*) > 1

-------------------------

SELECT *
FROM REQUESTDETAILS
WHERE CLIENTNAME = 'lluh'  and REQUEST_ID IN
(
      SELECT REQUEST_ID
      FROM REQUESTDETAILS
      where UPLOAD_DATE >= CONVERT(date,getdate()-2,101)
and CLIENTNAME = 'lluh'
      GROUP BY REQUEST_ID
      HAVING COUNT(*) > 1
)
ORDER BY REQUEST_ID

----------------------


WITH CTE (REQUEST_ID, DuplicateCount)
AS
(
SELECT REQUEST_ID,
ROW_NUMBER() OVER(PARTITION BY REQUEST_ID ORDER BY REQUEST_ID) AS DuplicateCount
FROM REQUESTDETAILS  where UPLOAD_DATE >= CONVERT(date,getdate()-2,101)
and CLIENTNAME = 'lluh'
)
DELETE