postgresql-删除重复数据保留一条

1.常规删除方法
explain analyse delete from deltest a where a.ctid <> (select min(t.ctid) from deltest t where a.id=t.id);
2.group by删除方法
explain analyse delete from deltest a where a.ctid not in (select min(ctid) from deltest group by id);
3.row_number删除方法
explain analyze delete from deltest a where a.ctid = any(array (select ctid from (select row_number() over (partition by id), ctid from deltest) t where t.row_number > 1));

根据某个字段分组删除重复数据,只保留日期最大的一条,建议使用窗口函数效率更高

posted @ 2021-06-10 18:50  月图灵  阅读(2878)  评论(0编辑  收藏  举报