删除 PostgreSQL 上的重复数据

Delete Duplicate Data on PostgreSQL

如何删除 table 上具有类型数据 like these 的重复数据。 我想在每个属性 id 上保持最新的 updated_at

喜欢如下:

attribute id | created at          | product_id
1            | 2020-04-28 15:31:11 | 112235
4            | 2020-04-28 15:30:25 | 112235
1            | 2020-04-29 15:30:25 | 112236
4            | 2020-04-29 15:30:25 | 112236

您可以使用 EXISTS 条件。

delete from the_table t1
where exists (select *
              from the_table t2
              where t2.created_at > t1.created_at
                and t2.attribute_id = t1.attribute_id);

这将删除所有存在具有更大 created_at 值的同一 attribute_id 的另一行的所有行(因此仅保留每个 [=12] 具有最高 created_at 的行=]).请注意,如果两个 created_at 值相同,则不会为 attribute_id

删除任何内容

Online example