我正在处理 postgres 查询以从表中删除重复项。下表是动态生成的,我想编写一个选择查询,如果第一行有重复值,它将删除记录。
表格看起来像这样
Ist col 2nd col
4 62
6 34
5 26
5 12
我想编写一个删除第 3 行或第 4 行的选择查询。
最佳答案
不需要中间表:
delete from df1
where ctid not in (select min(ctid)
from df1
group by first_column);
如果您要从一个大表中删除很多行,使用中间表的方法可能更快。
如果您只想获取一列的唯一值,您可以使用:
select distinct on (first_column) *
from the_table
order by first_column;
或者干脆
select first_column, min(second_column)
from the_table
group by first_column;
关于PostgreSQL 删除重复项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39928704/