The general way how to attack the dups in a table is as follows.
1) define the unique key column(s) - I use in my example KEY1, KEY2
2) define the column identifying the order - the highest value is preserved, all other values are considered as dups. I use ORDER1
Example
create table tab as
select 1 key1, 1 key2, 1 order1 from dual union all -- dup
select 1 key1, 1 key2, 2 order1 from dual union all -- dup
select 1 key1, 1 key2, 3 order1 from dual union all
select 1 key1, 2 key2, 1 order1 from dual union all
select 2 key1, 1 key2, 1 order1 from dual union all -- dup
select 2 key1, 1 key2, 2 order1 from dual union all
select 2 key1, 2 key2, 1 order1 from dual;
This query identifies the duplicated rows
select KEY1, KEY2, ORDER1 from
(select tab.*,
row_number() over (partition by key1, key2 order by order1 desc) as rn
from tab)
where rn > 1
KEY1 KEY2 ORDER1
---------- ---------- ----------
1 1 2
1 1 1
2 1 1
and this query deletes the dulicates
delete from tab where (KEY1, KEY2, ORDER1) in
(select KEY1, KEY2, ORDER1 from
(select tab.*,
row_number() over (partition by key1, key2 order by order1 desc) as rn
from tab)
where rn > 1)
Substitute your table and columns names for TAB
, KEY1, KEY2
and ORDER1
.