当所有元组的重复项和原始元组都相同时,如何删除重复项但保留一个?在 PostgreSQL 中

How to delete duplicates but keep one when all tuples are identical in duplicates and original? In postgreSQL

假设我们有以下 table.
如何删除2个重复项并保留一个?我的代码删除了所有这些。

+----+-------+
| ID | NAME  |
+----+-------+
|  2 | ARK   |
|  3 | CAR   |
|  9 | PAR   |
|  9 | PAR   |
|  9 | PAR   |
+----+-------+

理想情况下,您的 table 应该有一个唯一的 ID。如果没有,那么您可以使用 ctid 作为虚拟唯一 ID 字段作为以下查询。

ctid 表示行版本在其 table 中的物理位置。请注意,虽然 ctid 可用于非常快速地定位行版本,但如果行被 VACUUM FULL 更新或移动,其 ctid 将发生变化。因此 ctid 作为 long-term 行标识符是无用的。但它在这里完成了工作。

 delete from my_table a using my_table b where a=b and a.ctid < b.ctid;

DB fiddle link - https://dbfiddle.uk/?rdbms=postgres_10&fiddle=4888d519e125dc095496a57477a60b9f

你可以使用 deletion by row_number

    Delete from table t1 where 1<(
   Select rn from ( select id, name, 
   row_number() over (partition by id, name
  order by id) rn from table)