4 ways to delete duplicate records Oracle
SQL> select empno,count(empno) from emp21 group by empno
2 having count(empno)>1;
EMPNO COUNT(EMPNO)
---------- ------------
7782 2
7844 2
7698 2
7902 2
7566 2
7788 2
7654 2
7934 2
7876 2
7900 2
10 rows selected.
1. Using rowid
SQL> delete from emp21 e1
where rowid not in
(select max(rowid) from emp21 e2
where e1.empno = e2.empno );
2. Using self-join
SQL> delete from emp
where rowid not in
(select max(rowid) from emp group by empno);
3. Using row_number()
SQL> delete from emp21 where rowid in
(
select rid from
(
select rowid rid,
row_number() over(partition by empno order by empno) rn
from emp21
)
where rn > 1
);
4. Using dense_rank()
SQL> (
select rid from
(
select rowid rid,
dense_rank() over(partition by empno order by empno) rn
from emp21
)
where rn > 1
);
SQL> select empno,count(empno) from emp21 group by empno
2 having count(empno)>1;
EMPNO COUNT(EMPNO)
---------- ------------
7782 2
7844 2
7698 2
7902 2
7566 2
7788 2
7654 2
7934 2
7876 2
7900 2
10 rows selected.
1. Using rowid
SQL> delete from emp21 e1
where rowid not in
(select max(rowid) from emp21 e2
where e1.empno = e2.empno );
2. Using self-join
SQL> delete from emp
where rowid not in
(select max(rowid) from emp group by empno);
3. Using row_number()
SQL> delete from emp21 where rowid in
(
select rid from
(
select rowid rid,
row_number() over(partition by empno order by empno) rn
from emp21
)
where rn > 1
);
4. Using dense_rank()
SQL> (
select rid from
(
select rowid rid,
dense_rank() over(partition by empno order by empno) rn
from emp21
)
where rn > 1
);
No comments:
Post a Comment