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 |
| 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.