-- deleting duplicate rows
--using row id concept
drop table emp;
create table emp (empno number(6) , ename varchar2(20), sal number(8));
insert into emp values(100,'aaa',4000);
insert into emp values(200,'bbb',1000);
insert into emp values(300,'ccc',5000);
insert into emp values(400,'ddd',3000);
insert into emp values(500,'eee',400);
insert into emp values(100,'aaa',4000);
insert into emp values(200,'bbb',1000);
insert into emp values(300,'ccc',5000);
insert into emp values(400,'ddd',3000);
insert into emp values(500,'eee',400);
select * from emp order by empno;
-- RowID concept... Unique for each row...same row id
-- if rows are same
SELECT min(rowid)
FROM emp
group BY empno,ename,sal;
-- delete min row id. here we need to
-- list columns in group by clause
-- 5 rows deleted
DELETE FROM emp
WHERE rowid not in
(
SELECT MIN(rowid)
FROM emp
GROUP BY empno, ename, sal
) ;
-- 3 rows deleted
select * from emp order by empno;
No comments:
Post a Comment