开发者

sql server 2008 deleting duplicates

开发者 https://www.devze.com 2023-02-20 12:45 出处:网络
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 dupli开发者_如何学Gocates?WITHq AS

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 dupli开发者_如何学Gocates?


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


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;


insert into table_new
   Select col1, col2, min(pk) as pk from table_old
   group by col1, col2

-- debug table_new

drop table_old
0

精彩评论

暂无评论...
验证码 换一张
取 消