hari-p
2/19/2016 - 4:09 AM

postgresql duplicate

postgresql duplicate

A frequent question in IRC is how to delete rows that are duplicates over a set of columns, keeping only the one with the lowest ID.
This query does that for all rows of tablename having the same column1, column2, and column3.
DELETE FROM tablename
WHERE id IN (SELECT id
              FROM (SELECT id,
                             ROW_NUMBER() OVER (partition BY column1, column2, column3 ORDER BY id) AS rnum
                     FROM tablename) t
              WHERE t.rnum > 1);
Sometimes a timestamp field is used instead of an ID field.
SELECT internal_id
              FROM (SELECT internal_id,
                             ROW_NUMBER() OVER (partition BY list_id, object_id ORDER BY internal_id) AS rnum
                     FROM favourites_list_objects_copy1) t
              WHERE t.rnum > 1