Skip to content Skip to sidebar Skip to footer

Psql Get Duplicate Row

I have table like this- id object_id product_id 1 1 1 2 1 1 4 2

Solution 1:

If this is a one-off then you can simply identify the records you want to keep like so:

SELECT MIN(id) AS id
FROM yourtable
GROUPBY object_id, product_id;

You want to check that this works before you do the next thing and actually throw records out. To actually delete those duplicate records you do:

DELETEFROM yourtable WHERE id NOTIN (
  SELECTMIN(id) AS id
  FROM yourtable
  GROUPBY object_id, product_id
);

The MIN(id) obviously always returns the record with the lowest id for a set of (object_id, product_id). Change as desired.

Post a Comment for "Psql Get Duplicate Row"