Delete duplicate rows from table with no unique key

后端 未结 5 482
误落风尘
误落风尘 2021-01-13 09:46

How do I delete duplicates rows in Postgres 9 table, the rows are completely duplicates on every field AND there is no individual field that could be used as a unique key so

5条回答
  •  没有蜡笔的小新
    2021-01-13 10:25

    If you can afford to rewrite the whole table, this is probably the simplest approach:

    WITH Deleted AS (
      DELETE FROM discogs.releases_labels
      RETURNING *
    )
    INSERT INTO discogs.releases_labels
    SELECT DISTINCT * FROM Deleted
    

    If you need to specifically target the duplicated records, you can make use of the internal ctid field, which uniquely identifies a row:

    DELETE FROM discogs.releases_labels
    WHERE ctid NOT IN (
      SELECT MIN(ctid)
      FROM discogs.releases_labels
      GROUP BY label, release_id, catno
    )
    

    Be very careful with ctid; it changes over time. But you can rely on it staying the same within the scope of a single statement.

提交回复
热议问题