Skip to main content
Fixed table and reworded.
Source Link
James Risner
  • 6.1k
  • 11
  • 33
  • 60

To remove duplicates (keep only one entry) from a table "tab" where data looks like this:

fk_id_1fk_id_2
1232
1232
1232
1537
1537

| fk_id_1 | fk_id_2 |
| 12 | 32 |
| 12 | 32 |
| 12 | 32 |
| 15 | 37 |
| 15 | 37 |
you candYou can do this:

DELETE FROM tab WHERE ctid IN (SELECT ctid FROM (SELECT ctid, fk_id_1, fk_id_2, row_number() OVER (PARTITION BY fk_id_1, fk_id_2 ORDER BY fk_id_1) AS rnum FROM tab) t WHERE t.rnum > 1); 
DELETE FROM tab WHERE ctid IN (SELECT ctid FROM (SELECT ctid, fk_id_1, fk_id_2, row_number() OVER (PARTITION BY fk_id_1, fk_id_2 ORDER BY fk_id_1) AS rnum FROM tab) t WHERE t.rnum > 1); 

whereWhere ctid is the physical location of the row within its table (therefore, a row identifier) and row_number is a window function that assigns a sequential integer to each row in a result set.

PARTITION groups the result set and the sequential integer is restarted for every group.

To remove duplicates (keep only one entry) from a table "tab" where data looks like this:

| fk_id_1 | fk_id_2 |
| 12 | 32 |
| 12 | 32 |
| 12 | 32 |
| 15 | 37 |
| 15 | 37 |
you cand do this:

DELETE FROM tab WHERE ctid IN (SELECT ctid FROM (SELECT ctid, fk_id_1, fk_id_2, row_number() OVER (PARTITION BY fk_id_1, fk_id_2 ORDER BY fk_id_1) AS rnum FROM tab) t WHERE t.rnum > 1); 

where ctid is the physical location of the row within its table (therefore, a row identifier) and row_number is a window function that assigns a sequential integer to each row in a result set.

PARTITION groups the result set and the sequential integer is restarted for every group.

To remove duplicates (keep only one entry) from a table "tab" where data looks like this:

fk_id_1fk_id_2
1232
1232
1232
1537
1537

You can do this:

DELETE FROM tab WHERE ctid IN (SELECT ctid FROM (SELECT ctid, fk_id_1, fk_id_2, row_number() OVER (PARTITION BY fk_id_1, fk_id_2 ORDER BY fk_id_1) AS rnum FROM tab) t WHERE t.rnum > 1); 

Where ctid is the physical location of the row within its table (therefore, a row identifier) and row_number is a window function that assigns a sequential integer to each row in a result set.

PARTITION groups the result set and the sequential integer is restarted for every group.

Source Link

To remove duplicates (keep only one entry) from a table "tab" where data looks like this:

| fk_id_1 | fk_id_2 |
| 12 | 32 |
| 12 | 32 |
| 12 | 32 |
| 15 | 37 |
| 15 | 37 |
you cand do this:

DELETE FROM tab WHERE ctid IN (SELECT ctid FROM (SELECT ctid, fk_id_1, fk_id_2, row_number() OVER (PARTITION BY fk_id_1, fk_id_2 ORDER BY fk_id_1) AS rnum FROM tab) t WHERE t.rnum > 1); 

where ctid is the physical location of the row within its table (therefore, a row identifier) and row_number is a window function that assigns a sequential integer to each row in a result set.

PARTITION groups the result set and the sequential integer is restarted for every group.