I was asked to do a query to remove all duplicates that involve these 6 fields as we want a unique key in this table.
I need to leave the one with biggest ID on the table and delete all the other ones. I came up with the following code following the suggestion of using a for-loop to do it.
begin
for rec in (
select max(id), attribute, product, partner, pr_group, contact_person, branch, count(1) cnt
from co_attribute
where attribute = 100000034 and product = 100252046
group by attribute, product, partner, pr_group, contact_person, branch
having count(1) > 1
) loop
delete from co_attribute
where (attribute = rec.attribute
or (attribute is null and rec.attribute is null))
and (product = rec.product
or (product is null and rec.product is null))
and (partner = rec.partner
or(partner is null and rec.partner is null))
and (pr_group = rec.pr_group
or(pr_group is null and rec.pr_group is null))
and (contact_person = rec.contact_person
or(contact_person is null and rec.contact_person is null))
and (branch = rec.branch
or(branch is null and rec.branch is null))
and (max(id) < rec.id)
;
dbms_output.put_line(
'Deleting duplicates ' ||
' |attribute: ' || rec.attribute ||
', product: ' || rec.product ||
', partner: ' || rec.partner ||
', pr_group: ' || rec.pr_group ||
', contact_person: ' || rec.contact_person ||
', branch: ' || rec.branch ||
', id: ' || rec.id ||
', deleting row: ' || sql%rowcount
);
end loop;
end;
/
The where in the select is just so I don't mess up the entire DB while testing this script. This gives me the following errors:
Error report -
ORA-06550: line 22, column 15:
PL/SQL: ORA-00934: group function is not allowed here
ORA-06550: line 9, column 7:
PL/SQL: SQL Statement ignored
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:
So I have two questions: first is, how do I solve this issue and is this the most effective way?
edit: removed the id field inside group by
You may use below statement to leave the one with biggest ID on the table and delete all the other ones :
delete co_attribute a
where
a.id <
any (select b.id
from co_attribute b
where (a.attribute = b.attribute or (a.attribute is null and b.attribute is null))
and (a.product = b.product or (a.product is null and b.product is null))
and (a.partner = b.partner or (a.partner is null and b.partner is null))
and (a.pr_group = b.pr_group or (a.pr_group is null and b.pr_group is null))
and (a.contact_person = b.contact_person or (a.contact_person is null and b.contact_person is null))
and (a.branch = b.branch or (a.branch is null and b.branch is null))
);
I fixed it following a part of the comments and a few changes suggested over here! First I changed max(id)
to max(id) maxid
and also max(id) < rec.id
to id < rec.maxid
since maxid
is part of rec
and not of the delete query!
Thanks a lot for the help!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.