Monday, October 12, 2009

Queries to identify/remove duplicate rows from a table

queries to identify or remove duplicate rows from a table
==========================================================
1.
DELETE FROM table_name A WHERE ROWID > (
SELECT min(rowid) FROM table_name B
WHERE A.key_values = B.key_values);

[hints:- Delete all rowids that is BIGGER than the SMALLEST rowid value
(for a given key)]

2. delete from my_table t1
where exists (select 'x' from my_table t2
where t2.key_value1 = t1.key_value1
and t2.key_value2 = t1.key_value2
and t2.rowid > t1.rowid);

3. delete from where rowid not in
( select min(rowid)
from exp group by column1..,column2,...column3..);

4. Column value duplicate
=========================
DELETE scott.emp
WHERE rowid IN
( SELECT LEAD(rowid) OVER
(PARTITION BY ename ORDER BY NULL)
FROM scott.emp );


5. This statement deletes rows from the emp table where duplicate values of
last_name appear.
======================================================

delete from emp e
where empno in(select empno from emp d
where d.last_name=e.last_name
minus
select empno from emp f
where f.last_name=e.last_name
and rownum=1)

No comments: