Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Delete duplicate based on condition

zephyr223Jun 8 2020 — edited Jun 10 2020

Good Afternoon everyone,

I have a table with duplicates, and what I am trying to do is delete duplicates based on the ID column but keep the record where the reportnumber is the min length.

So here is my setup:

CREATE TABLE PERSON

(id number,

reportnumber varchar2(20));

INSERT INTO person (id, reportnumber) VALUES('123','SER0AYY');

INSERT INTO person (id, reportnumber) VALUES('123','SER0AYY3455');

INSERT INTO person (id, reportnumber) VALUES('444','S0CRL111');

INSERT INTO person (id, reportnumber) VALUES('444','S0CRL');

INSERT INTO person (id, reportnumber) VALUES('444','S0CRL33');

INSERT INTO person (id, reportnumber) VALUES('555','S0A3V55');

INSERT INTO person (id, reportnumber) VALUES('555','S0A3V');

PERSON

idreportnumber
123SER0AYY
123SER0
444S0CRL111
444S0CRL
444S0CRL33
555S0A3V55
555S0A3V

After the delete, I would like to keep the following data: (the reportnumber column should be the value with the minimum lenght perr id number)

123

SER0

444S0CRL
555
S0A3V

I have the query for the duplicates:

select id, count(*)

from person

group by id

having count(*) >1;

Delete from person where rowid not in (

select max(rowid) from rowid group by id );

That works fine to delete duplicates randomly, but I do not know how to delete where the value of the reportnumber column is not the minimum length.  Any help is appreciated.  Thank you in advance.

This post has been answered by Frank Kulash on Jun 8 2020
Jump to Answer
Comments
Post Details
Added on Jun 8 2020
13 comments
4,494 views