select MyId,COUNT(1) from #MyTable
group by MyId
having COUNT(1) > 1
--Select duplicate records
with CTE
AS
(SELECT MyId, RANK() OVER (PARTITION BY MyId ORDER BY num) rnk
FROM(SELECT *, ROW_NUMBER() OVER(ORDER BY MyId) num
FROM #MyTable
) X
)
Select * from CTE
WHERE rnk > 1
--Delete Duplicate records
with CTE
AS
(SELECT MyId, RANK() OVER (PARTITION BY MyId ORDER BY num) rnk
FROM(SELECT *, ROW_NUMBER() OVER(ORDER BY MyId) num
FROM #MyTable
) X
)
delete from CTE
WHERE rnk > 1
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment