0

I need to delete duplicated values of this table:

+----+-------+-------------+---------+
| id | name  | description | surname |
+----+-------+-------------+---------+
| 1  | Peter | Member      | Hitsh   |
| 2  | James | Member      | Tach    |
| 3  | Mary  | Member      | Popims  |
| 4  | Peter | Member      | Hitsh   |
+----+-------+-------------+---------+

I would want to remove all the duplicated values with the same name and surname.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Za7pi
  • 1,338
  • 7
  • 22
  • 33

1 Answers1

0

To keep the row with the smallest id for every set of duplicates on (name, surname):

DELETE FROM tbl
USING (
   SELECT id, row_number OVER (PARTITION BY name, surname ORDER BY id) As rn
   FROM   tbl
   ) del
WHERE  tbl.id = del.id
AND    del.rn > 1;

Assuming id to be unique.

Very similar question today:
Delete all rows but one with the greatest value per group

Community
  • 1
  • 1
Erwin Brandstetter
  • 605,456
  • 145
  • 1,078
  • 1,228