删除重复记录,保留一个

时间:2011-08-23 14:18:31

标签: sql postgresql

我有一个从CSV文件中copy创建的临时表,结果包含一些重复的ID。我需要删除任何重复。我尝试过以下方法:

delete from my_table where id in
    (select id from (select count(*) as count, id
        from my_table group by id) as counts where count>1);

然而,这会删除重复的记录,我必须保留一个。

如何只删除带有重复ID的第二条记录?

感谢。

2 个答案:

答案 0 :(得分:2)

您的查询会删除计数大于1的所有ID,因此会删除所有重复的ID。您需要做的是从重复列表中隔离一条记录并保留:

delete
from   my_table
where  id in     (select   id
                  from     my_table
                  where    some_field in (select   some_field
                                          from     my_table
                                          group by some_field
                                          having   count(id) > 1))
and    id not in (select   min(id)
                  from     my_table
                  where    some_field in (select   some_field
                                          from     my_table
                                          group by some_field
                                          having   count(id) > 1)
                  group by some_field);

编辑修正:P

答案 1 :(得分:1)

假设你没有外键关系......

CREATE TABLE "temp"(*column definitions*);

insert into "temp" (*column definitions*)
select *column definitions*
from (
        select *,row_number() over(PARTITION BY id) as rn from "yourtable"
) tm
where rn=1;

drop table "yourtable";

alter table "temp" rename to "yourtable";