ORALCE 的四种去重方法

例如:创建一个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博客 ” ,链接:http://blog.itpub.net/29366942/viewspace-1062240/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/29366942/viewspace-1062240/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值