sql server 2008删除重复项

时间:2011-03-28 15:33:45

标签: sql sql-server sql-server-2008

我有像这样的重复

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

每个只有两个。如何删除重复项?

3 个答案:

答案 0 :(得分: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

见这里:

答案 1 :(得分:1)

如果原始表不是很大。

select distinct * from origin_table into temp_table;
truncate table origin_table;
insert into origin_table select * from temp_table ;
drop table temp_table;

答案 2 :(得分: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
相关问题