Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Trying to delete duplicate rows by using rownum in Oracle 8.1.7.2.0

samujjwalbasuFeb 4 2002
Hi Everybody
I am using Oracle 8.1.7.2.0 and trying to delete the duplicate rows.

The script I am using is as follows:

drop table abc;

create table abc
(num_col number(1),
char_col varchar2(10));

insert into abc
values (1,'VIDYA');

insert into abc
values (2,'BASU');

insert into abc
values (1,'VIDYA');

insert into abc
values (1,'VIDYA');

insert into abc
values (1,'VIDYA');

commit;

select * from abc;

delete abc
where num_col = 1
and (num_col,rownum) in
(
select num_col,r_num
from (
select num_col,rownum r_num
from abc
where num_col = 1
)
minus
select num_col,r_num
from (
select num_col,max(r_num) r_num
from (
select num_col,rownum r_num
from abc
where num_col = 1
)
group by num_col
)
)
/
commit;
select * from abc;


And the output of this script is comming like this:

SQL>drop table abc;

Table dropped.

SQL>
SQL>create table abc
2 (num_col number(1),
3 char_col varchar2(10));

Table created.

SQL>
SQL>insert into abc
2 values (1,'VIDYA');

1 row created.

SQL>
SQL>insert into abc
2 values (2,'BASU');

1 row created.

SQL>
SQL>insert into abc
2 values (1,'VIDYA');

1 row created.

SQL>
SQL>insert into abc
2 values (1,'VIDYA');

1 row created.

SQL>
SQL>insert into abc
2 values (1,'VIDYA');

1 row created.

SQL>
SQL>commit;

Commit complete.

SQL>
SQL>select * from abc;

NUM_COL CHAR_COL
--------------- ----------
1 VIDYA
2 BASU
1 VIDYA
1 VIDYA
1 VIDYA

SQL>
SQL>delete abc
2 where num_col = 1
3 and (num_col,rownum) in
4 (
5 select num_col,r_num
6 from (
7 select num_col,rownum r_num
8 from abc
9 where num_col = 1
10 )
11 minus
12 select num_col,r_num
13 from (
14 select num_col,max(r_num) r_num
15 from (
16 select num_col,rownum r_num
17 from abc
18 where num_col = 1
19 )
20 group by num_col
21 )
22 )
23 /

3 rows deleted.

SQL>commit;

Commit complete.

SQL>select * from abc;

NUM_COL CHAR_COL
--------------- ----------
2 BASU
1 VIDYA
1 VIDYA
1 VIDYA

SQL>

Could anybody explai me what is going on. Because it is showing that it has deleted 3 rows. But When I am selecting I am seeing that it has only delete 1 row.

Thank You in advance.
Samujjwal Basu
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 4 2002
Added on Feb 4 2002
6 comments
444 views