How to find duplicate records in PostgreSQL

前端 未结 4 1424
长情又很酷
长情又很酷 2021-01-29 17:17

I have a PostgreSQL database table called \"user_links\" which currently allows the following duplicate fields:

year, user_id, sid, cid

The uni

4条回答
  •  生来不讨喜
    2021-01-29 17:44

    You can join to the same table on the fields that would be duplicated and then anti-join on the id field. Select the id field from the first table alias (tn1) and then use the array_agg function on the id field of the second table alias. Finally, for the array_agg function to work properly, you will group the results by the tn1.id field. This will produce a result set that contains the the id of a record and an array of all the id's that fit the join conditions.

    select tn1.id,
           array_agg(tn2.id) as duplicate_entries, 
    from table_name tn1 join table_name tn2 on 
        tn1.year = tn2.year 
        and tn1.sid = tn2.sid 
        and tn1.user_id = tn2.user_id 
        and tn1.cid = tn2.cid
        and tn1.id <> tn2.id
    group by tn1.id;
    

    Obviously, id's that will be in the duplicate_entries array for one id, will also have their own entries in the result set. You will have to use this result set to decide which id you want to become the source of 'truth.' The one record that shouldn't get deleted. Maybe you could do something like this:

    with dupe_set as (
    select tn1.id,
           array_agg(tn2.id) as duplicate_entries, 
    from table_name tn1 join table_name tn2 on 
        tn1.year = tn2.year 
        and tn1.sid = tn2.sid 
        and tn1.user_id = tn2.user_id 
        and tn1.cid = tn2.cid
        and tn1.id <> tn2.id
    group by tn1.id
    order by tn1.id asc)
    select ds.id from dupe_set ds where not exists 
     (select de from unnest(ds.duplicate_entries) as de where de < ds.id)
    

    Selects the lowest number ID's that have duplicates (assuming the ID is increasing int PK). These would be the ID's that you would keep around.

提交回复
热议问题