例如:创建一个test测试表:
create table test (c1 int ,c2 varchar2(10));
insert into test values (1,'Smith');
insert into test values (1,'Smith');
insert into test values (2,'John');
insert into test values(1,'Jack');
insert into test values (2,'Jack');
insert into test values (2,'John');
insert into test values (2,'John');
commit;
一种方法: distinct 把之前的表去重显示并创建,drop table old_table;
create table tmp_test as select distinct * from test1; ---创建临时表
drop table test1;
alter table tmp_test rename to test1;
第二种 rowid
delete from test
where rowid <> ( select min(rowid)
from test b
where b.c1 = test.c1
and b.c2 = test.c2 )
第三种方法:分组,rowid
delete from test t where t.rowid not in (select min(rowid) from test group by c1,c2 );
commit;
delete from test t where t.rowid not in (select min(rowid) from test a where t.c1=a.c1 and t.c2=a.c2);
Rowid为伪列 是物理地址
OOOOOO: 数据库对象号
FFF: 表空间相关的数据文件号(相对文件号)
BBBBBB: 数据块号
RRR: 在块中的行号
第四种方法,分析函数 dense_rank()
drop table test;
create table test (c1 int ,c2 varchar2(10));
insert into test values (1,'Smith');
insert into test values (1,'Smith');
insert into test values (2,'John');
insert into test values(1,'Jack');
insert into test values (2,'Jack');
insert into test values (2,'John');
insert into test values (2,'John');
commit;
select c1,c2,rowid rd,row_number() over(partition by c1,c2 order by c1) rn
from test;
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/29366942/viewspace-1062240/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/29366942/viewspace-1062240/