Delete duplicate rows from table

前端 未结 2 660
灰色年华
灰色年华 2021-01-15 13:59

I have unique keys id keys in my table but I have a column with duplicate values? how do I get rid of those, while preserving only one of them like this :

Duplicate

2条回答
  •  庸人自扰
    2021-01-15 15:06

    Using Rank, actually I'm not totally sure about the syntax because I'm not that good at PostgreSQL, this is just a hint anyway (anybody's correction will be appreciated):

    DELETE FROM mytable
    WHERE id NOT IN
    (
       SELECT x.id FROM
       (
          SELECT id, RANK() OVER (PARTITION BY name ORDER BY id ASC) AS r
          FROM mytable
       ) x
       WHERE x.r = 1
    )
    

提交回复
热议问题