Usa KEEP LAST
di Oracle per trovare il miglior rc_document_id
per document_id
. Quindi elimina tutti gli altri.
delete from rc_document
where rc_document_id not in
(
select max(d.rc_document_id) keep (dense_rank last order by dg.priority)
from rc_document d
join rc_document_group dg using (rc_document_group_id)
group by d.document_id
);
Demo Rextester:http://rextester.com/NZVZGF52818