ITPub博客

首页 > 数据库 > Oracle > ORALCE 的四种去重方法

ORALCE 的四种去重方法

原创 Oracle 作者:THANK_DBA 时间:2013-12-04 21:07:46 0 删除 编辑

例如:创建一个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为伪列   是物理地址

oracle比较函数 decode/case when~~~4种去重方法~~~去重中的统计函数

 

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;

oracle比较函数 decode/case when~~~4种去重方法~~~去重中的统计函数 

不重复的只有1
重复的就会出现2,3,4

第一次出现,不重复的时候,rn为1
相同的记录,重复出现,第二次,就记录为2
第三次,3

怎么找出重复的行

select b.c1,b.c2 from 

    (select c1,c2,rowid rd,row_number() over(partition by c1,c2 order by c1) rn 

     from test) b 

where b.rn = 1;

 

 

oracle比较函数 decode/case when~~~4种去重方法~~~去重中的统计函数


 

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/29366942/viewspace-1062240/ ,如需转载,请注明出处,否则将追究法律责任。

下一篇: ROWID简介
请登录后发表评论 登录
全部评论

注册时间:2013-12-04

  • 博文量
    16
  • 访问量
    62966