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
  • 413,100
  • 91
  • 616
  • 614
JOE SKEET
  • 7,950
  • 14
  • 48
  • 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
  • 413,100
  • 91
  • 616
  • 614
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,767
  • 1
  • 14
  • 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,531
  • 1
  • 24
  • 43