I have a table that looks like this:
id | creation_date | col1 | col2 | col3 |
---|---|---|---|---|
1 | 21 April | a | b | e |
2 | 23 April | a | b | e |
3 | 21 April | c | d | e |
My goal is to delete all the duplicates, keeping only the first (ie first created) row.
The duplicates are defined using a set of columns from the table. For example, using col1 and col2, the result will be :
id | creation_date | col1 | col2 | col3 |
---|---|---|---|---|
1 | 21 April | a | b | e |
3 | 21 April | c | d | e |
One answer that I found use one column as a key :
DELETE FROM dups a USING (
SELECT MIN(ctid) as ctid, key
FROM dups
GROUP BY key HAVING COUNT(*) > 1
) b
WHERE a.key = b.key
AND a.ctid <> b.ctid
, but I can't see how to adapt to the case where multiple columns are used, and where the earliest record should be kept.