Your technology certification is waiting. Enroll in Cloud Class ®
CREATE TABLE DeleteDuplicateUsingOuterJoin
INSERT INTO DeleteDuplicateUsingOuterJoin
;WITH CTE AS
SELECT * , ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) rnk FROM DeleteDuplicateUsingOuterJoin
FROM CTE p
LEFT OUTER JOIN
SELECT MIN(rnk) ID FROM CTE
GROUP BY ID
)u ON u.ID = p.rnk
WHERE u.ID IS NULL
Open in new window
CREATE TABLE DeleteDup
INSERT INTO DeleteDup VALUES(1),(2),(1)
SELECT ID FROM DeleteDup
/************ SOLUTION 1 ****************/
WITH CTE AS
SELECT *, ROW_NUMBER() OVER ( PARTITION BY ID ORDER BY ID ) RNK FROM DeleteDup
DELETE FROM CTE WHERE RNK > 1
select col1, col2, count(*)
group by col1, col2 having count(*)>1
Deleting duplicates can be hard work, and I think you have covered off the more popular approaches pretty well.
I also like the importance of taking a backup first :)
Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.
Have a better answer? Share it in a comment.
Please enter a first name
Please enter a last name
Must be at least 4 characters long.
Join and Comment
From novice to tech pro — start learning today.
Premium members can enroll in this course at no extra cost.