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

mysql delete duplicate rows from table

From

Delete duplicate rows from a BigQuery table

From

Delete duplicate rows from small table

From Dev

Delete Oldest Duplicate Rows from a BigQuery 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

Delete duplicate rows in a large table

From Dev

Is it possible to delete duplicate rows from a table without knowing it's columns?

From Dev

How to delete duplicate rows in a table based on what is supposed to be a unique column referring to a table in another database

From Dev

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

From

How to delete duplicate rows without unique identifier

From Dev

Delete duplicate rows from dataset

From Dev

MySQL: Altering a table and delete duplicate unique keys

From Dev

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

From Dev

How to delete duplicate rows in MySQL table?

From Dev

Delete duplicate rows in mySQL in same 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 Table based multiple Columns check and Table size remains same

From Dev

Selecting unique rows from a table having duplicate values in only one column

From Dev

Delete duplicate rows from Multidimensional Object array [,]?

From Dev

Delete duplicate rows from SELECT query?

From Dev

How to delete duplicate rows from mysql

From Dev

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

From Dev

Delete X rows from table

From Mysql

MySQL update, but delete on duplicate key (due to unique index)

From Dev

How do I introduce a constraint in django table where values from 2 columns will be unique in a table and other rows can have duplicate pairs?

From Dev

Duplicate table rows based on a count and assign unique ids

From Dev

Highlight duplicate rows based on two columns in table, with unique colours for pairs

From Dev

R data.table merge duplicate rows and concatenate unique values

Related Related

  1. 1

    mysql delete duplicate rows from table

  2. 2

    Delete duplicate rows from a BigQuery table

  3. 3

    Delete duplicate rows from small table

  4. 4

    Delete Oldest Duplicate Rows from a BigQuery Table

  5. 5

    Delete duplicate rows in a table

  6. 6

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

  7. 7

    Delete duplicate rows in a large table

  8. 8

    Is it possible to delete duplicate rows from a table without knowing it's columns?

  9. 9

    How to delete duplicate rows in a table based on what is supposed to be a unique column referring to a table in another database

  10. 10

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

  11. 11

    How to delete duplicate rows without unique identifier

  12. 12

    Delete duplicate rows from dataset

  13. 13

    MySQL: Altering a table and delete duplicate unique keys

  14. 14

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

  15. 15

    How to delete duplicate rows in MySQL table?

  16. 16

    Delete duplicate rows in mySQL in same table

  17. 17

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

  18. 18

    Delete Duplicate Rows from Table based multiple Columns check and Table size remains same

  19. 19

    Selecting unique rows from a table having duplicate values in only one column

  20. 20

    Delete duplicate rows from Multidimensional Object array [,]?

  21. 21

    Delete duplicate rows from SELECT query?

  22. 22

    How to delete duplicate rows from mysql

  23. 23

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

  24. 24

    Delete X rows from table

  25. 25

    MySQL update, but delete on duplicate key (due to unique index)

  26. 26

    How do I introduce a constraint in django table where values from 2 columns will be unique in a table and other rows can have duplicate pairs?

  27. 27

    Duplicate table rows based on a count and assign unique ids

  28. 28

    Highlight duplicate rows based on two columns in table, with unique colours for pairs

  29. 29

    R data.table merge duplicate rows and concatenate unique values

HotTag

Archive