I am attempting to address these duplicates without removing the second entry. I am seeking a solution where duplicate records of an identifier would be assigned the maximum identifier value for the account_id.
select account_id, identifier, count(*)
from books
group by account_id, identifier
HAVING count(*) > 1;
account_id | identifier | count
------------+------------+-------
111 | 155 | 2
111 | 198 | 2
111 | 178 | 2
111 | 167 | 2
111 | 196 | 2
111 | 156 | 2
111 | 150 | 2
111 | 223 | 2
For Example: (Processing only the first record)
(Consider the maxidentifier as 223 based on the account_id)
For the below record
account_id | identifier | count
------------+------------+-------
111 | 155 | 2
it should set the identifier as (max_identifier+1)
account_id | identifier | count
------------+------------+-------
111 | 155 | 1
111 | 224 | 1
Likewise it should do this in the loop for all the records without breaking other Identifiers and records.
#
# Table name: books
#
# id :bigint not null, primary key
# account_id :bigint not null
# identifier :bigint not null
# Indexes
# unique_account_identifier (account_id,identifier) UNIQUE
2
Answers
This resolved the Duplicate issue without Deleting.This Query updates the duplicates with the max_identifier of an acocunt_id.
This should do it:
(online demo)