3

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?

Quassnoi
  • 398,504
  • 89
  • 603
  • 604
JOE SKEET
  • 7,786
  • 13
  • 46
  • 64

3 Answers3

12
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:

Quassnoi
  • 398,504
  • 89
  • 603
  • 604
1

If the origin table is not huge.

select distinct * from origin_table into temp_table;
truncate table origin_table;
insert into origin_table select * from temp_table ;
drop table temp_table;
RollingBoy
  • 2,727
  • 1
  • 13
  • 8
0
insert into table_new
   Select col1, col2, min(pk) as pk from table_old
   group by col1, col2

-- debug table_new

drop table_old
Beth
  • 9,425
  • 1
  • 21
  • 41