Delete duplicate rows from table with no unique key

Paul Taylor

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 I cant just GROUP BY columns and use a NOT IN statement.

I'm looking for a single SQL statement, not a solution that requires me to create temporary table and insert records into that. I know how to do that but requires more work to fit into my automated process.

Table definition:

jthinksearch=> \d releases_labels;
Unlogged table "discogs.releases_labels"
   Column   |  Type   | Modifiers
------------+---------+-----------
 label      | text    |
 release_id | integer |
 catno      | text    |
Indexes:
    "releases_labels_catno_idx" btree (catno)
    "releases_labels_name_idx" btree (label)
Foreign-key constraints:
    "foreign_did" FOREIGN KEY (release_id) REFERENCES release(id)

Sample data:

jthinksearch=> select * from releases_labels  where release_id=6155;
    label     | release_id |   catno
--------------+------------+------------
 Warp Records |       6155 | WAP 39 CDR
 Warp Records |       6155 | WAP 39 CDR
Nick Barnes

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to delete duplicate rows in sybase, when you have no unique key?

From Dev

Delete duplicate rows from a BigQuery table

From Dev

mysql delete duplicate rows from table

From Dev

Delete duplicate rows in a table

From Dev

SQL Delete duplicate rows in the table without primary key on SQL Server

From Dev

MySQL delete duplicate rows in table

From Dev

Delete duplicate records from a Postgresql table without a primary key?

From Dev

How to delete duplicate rows without unique identifier

From Dev

Delete duplicate rows from dataset

From Dev

Delete rows from table

From Dev

MySQL - Add duplicate rows to archive table, then delete duplicate rows

From Dev

how to create a table with only the duplicate rows and delete the duplicates from the main table?

From Dev

how to create a table with only the duplicate rows and delete the duplicates from the main table?

From Dev

Delete duplicate rows from two dimentsional array

From Dev

How to delete duplicate rows from mysql

From Dev

Delete duplicate rows from Multidimensional Object array [,]?

From Dev

Query to delete all duplicate rows but one where no columns is/are unique

From Dev

Delete X rows from table

From Dev

insert ... select ... on duplicate key update + delete obsolete rows

From Dev

Duplicate table rows based on a count and assign unique ids

From Dev

How to delete duplicate records from a table in oracle

From Dev

Unique duplicate rows with range

From Dev

SQL - Remove Duplicate Rows From Table

From Dev

Find Duplicate Rows/Records from Table

From Dev

DB2: Can not delete rows from empty table after it was referenced in foreign key

From Dev

DB2: Can not delete rows from empty table after it was referenced in foreign key

From Dev

Return only unique rows from a Table

From Dev

Delete duplicate rows in calc?

From Dev

Delete duplicate rows in mysql

Related Related

  1. 1

    How to delete duplicate rows in sybase, when you have no unique key?

  2. 2

    Delete duplicate rows from a BigQuery table

  3. 3

    mysql delete duplicate rows from table

  4. 4

    Delete duplicate rows in a table

  5. 5

    SQL Delete duplicate rows in the table without primary key on SQL Server

  6. 6

    MySQL delete duplicate rows in table

  7. 7

    Delete duplicate records from a Postgresql table without a primary key?

  8. 8

    How to delete duplicate rows without unique identifier

  9. 9

    Delete duplicate rows from dataset

  10. 10

    Delete rows from table

  11. 11

    MySQL - Add duplicate rows to archive table, then delete duplicate rows

  12. 12

    how to create a table with only the duplicate rows and delete the duplicates from the main table?

  13. 13

    how to create a table with only the duplicate rows and delete the duplicates from the main table?

  14. 14

    Delete duplicate rows from two dimentsional array

  15. 15

    How to delete duplicate rows from mysql

  16. 16

    Delete duplicate rows from Multidimensional Object array [,]?

  17. 17

    Query to delete all duplicate rows but one where no columns is/are unique

  18. 18

    Delete X rows from table

  19. 19

    insert ... select ... on duplicate key update + delete obsolete rows

  20. 20

    Duplicate table rows based on a count and assign unique ids

  21. 21

    How to delete duplicate records from a table in oracle

  22. 22

    Unique duplicate rows with range

  23. 23

    SQL - Remove Duplicate Rows From Table

  24. 24

    Find Duplicate Rows/Records from Table

  25. 25

    DB2: Can not delete rows from empty table after it was referenced in foreign key

  26. 26

    DB2: Can not delete rows from empty table after it was referenced in foreign key

  27. 27

    Return only unique rows from a Table

  28. 28

    Delete duplicate rows in calc?

  29. 29

    Delete duplicate rows in mysql

HotTag

Archive