架构理念:[简单][高效][可依赖] 管理理念:[价值][勇气][专注]

remove duplicates in Postgres(sql去重)

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.

posted @ 2016-04-14 10:17  文和-Mignet  阅读(416)  评论(0编辑  收藏  举报