How to remove duplicate records from Oracle table.
To remove duplicate rows before the primary key or unique indexes are created:
1.
Suppose you have one identifying key filed (aKey)
This script deletes each row in the group after the first row
DELETE FROM aTable
WHERE rowid NOT IN
(SELECT MIN(rowid)
FROM aTable
GROUP BY aKey);
2.
Using a correlated subquery
DELETE FROM aTable t1
WHERE rowid > (
SELECT MIN(rowid)
FROM aTable t2
WHERE t1.aKey = t2.aKey)
3.
Using an analytic function
DELETE FROM aTable WHERE rowid IN
(
SELECT rowid FROM
(
SELECT rowid, ROW_NUMBER () OVER (PARTITION BY aKey ORDER BY rowid) r_no
FROM your_table
)
WHERE r_no > 1
)