首页数据库Oracle去除重复数据的方法是什么?一文看懂Oracle去重

Oracle去除重复数据的方法是什么?一文看懂Oracle去重

时间2024-03-24 23:04:03发布访客分类数据库浏览913
导读:这篇文章给大家分享的是Oracle去除重复数据的方法,Oracle去重是比较实用的操作,文中示例代码介绍的非常详细,对大家学习Oracle数据库的使用有一定的帮助,感兴趣的朋友接下来一起跟随小编看看吧。 oracle去除重复数据...


这篇文章给大家分享的是Oracle去除重复数据的方法,Oracle去重是比较实用的操作,文中示例代码介绍的非常详细,对大家学习Oracle数据库的使用有一定的帮助,感兴趣的朋友接下来一起跟随小编看看吧。

oracle去除重复数据的方法:

创建测试数据

create table nayi224_180824(col_1 varchar2(10), col_2 varchar2(10), col_3 varchar2(10));
    
insert into nayi224_180824select 1, 2, 3 from dual 
union allselect 1, 2, 3 from dual 
union allselect 5, 2, 3 from dual 
union allselect 10, 20, 30 from dual ;
    
commit;
    select*from nayi224_180824;
    
COL_1 COL_2 COL_3
1 2 3
1 2 3
5 2 3
10 20 30

针对指定列,查出去重后的结果集

distinct

select distinct t1.* from nayi224_180824 t1;
    
COL_1 COL_2 COL_3
10 20 30
1 2 3
5 2 3

方法局限性很大,因为它只能对全部查询的列做去重。如果我想对col_2,col3去重,那我的结果集中就只能有col_2,col_3列,而不能有col_1列。

select distinct t1.col_2, col_3 from nayi224_180824 t1
COL_2 COL_3
2 3
20 30

不过它也是最简单易懂的写法。

row_number()

select *  from (select t1.*,
               row_number() over(partition by t1.col_2, t1.col_3 order by 1) rn          
               from nayi224_180824 t1) t1 where t1.rn = 1;
    
COL_1 COL_2 COL_3 RN
1 2 3 1
10 20 30 1

写法上要麻烦不少,但是有更大的灵活性。

针对指定列,查出所有重复的行

count having

select *  from nayi224_180824 t 
where (t.col_2, t.col_3) in (select t1.col_2, t1.col_3                                
from nayi224_180824 t1                               
group by t1.col_2, t1.col_3                              
having count(1) >
     1)
COL_1 COL_2 COL_3
1 2 3
1 2 3
5 2 3

要查两次表,效率会比较低。不推荐。

count over

select *  from (select t1.*,               
count(1) over(partition by t1.col_2, t1.col_3) rn          
from nayi224_180824 t1) t1 where t1.rn >
     1;
    
COL_1 COL_2 COL_3 RN
1 2 3 3
1 2 3 3
5 2 3 3

只需要查一次表,推荐。

删除所有重复的行

delete from nayi224_180824 t where t.rowid in (                   
select rid                     
from (select t1.rowid rid,                                   
count(1) over(partition by t1.col_2, t1.col_3) rn                              
from nayi224_180824 t1) t1                    
where t1.rn >
     1);
    

就是上面的语句稍作修改。

删除重复数据并保留一条

分析函数法

delete from nayi224_180824 t where t.rowid in (select rid                     
from (select t1.rowid rid,
    
    row_number() over(partition by t1.col_2, t1.col_3 order by 1) rn                             
    from nayi224_180824 t1) t1                    
    where t1.rn >
     1);
    

拥有分析函数一贯的灵活性高的特点。可以为所欲为的分组,并通过改变orderby从句来达到像”保留最大id“这样的要求。

group by

delete from nayi224_180824 t where t.rowid not in
       (select max(rowid) from nayi224_180824 t1 group by t1.col_2, t1.col_3);
    

牺牲了一部分灵活性,换来了更高的效率。

关于Oracle去重的方法及操作步骤就介绍到这,上述示例具有一定的借鉴价值,感兴趣的朋友可以参考,希望能对大家有帮助,想要了解更多Oracle去重的内容,大家可以关注其它的相关文章。




文本转载自PHP中文网

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!


若转载请注明出处: Oracle去除重复数据的方法是什么?一文看懂Oracle去重
本文地址: https://pptw.com/jishu/652377.html
PHP中引用文件的方法有几种,有什么区别吗 PHP字符串下标怎么获取,有哪些应用

游客 回复需填写必要信息