Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sql server 2008 deleting duplicates

i have duplicates like this

col1, col2
1, alex
1, alex
2, liza
2, liza
3, peter
3, peter

there are only two of each. how do i delete the duplicates?

like image 372
JOE SKEET Avatar asked Dec 06 '25 16:12

JOE SKEET


1 Answers

WITH    q AS
        (
        SELECT  *,
                ROW_NUMBER() OVER (PARTITION BY col1, col2 ORDER BY col1, col2) AS rn
        FROM    mytable
        )
DELETE
FROM    q
WHERE   rn >= 2

See here:

  • Deleting duplicates
like image 134
Quassnoi Avatar answered Dec 08 '25 09:12

Quassnoi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!