oracle数据库去除重复数据


Posted in Oracle onMay 20, 2022

创建测试数据

create table nayi224_180824(col_1 varchar2(10), col_2 varchar2(10), col_3 varchar2(10));
insert into nayi224_180824
select 1, 2, 3 from dual union all
select 1, 2, 3 from dual union all
select 5, 2, 3 from dual union all
select 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数据库去除重复数据常用的文章就介绍到这了!


Tags in this post...

Oracle 相关文章推荐
Oracle笔记
Apr 05 Oracle
Oracle 数据仓库ETL技术之多表插入语句的示例详解
Apr 12 Oracle
oracle表分区的概念及操作
Apr 24 Oracle
Oracle设置DB、监听和EM开机启动的方法
Apr 25 Oracle
使用springboot暴露oracle数据接口的问题
May 07 Oracle
ORACLE查看当前账号的相关信息
Jun 18 Oracle
SQL模糊查询报:ORA-00909:参数个数无效问题的解决
Jun 21 Oracle
Oracle以逗号分隔的字符串拆分为多行数据实例详解
Jul 16 Oracle
Oracle 触发器trigger使用案例
Feb 24 Oracle
Oracle使用别名的好处
Apr 19 Oracle
排查并解决Oracle sysaux表空间异常增长
Apr 20 Oracle
Oracle中DBLink的详细介绍
Apr 29 Oracle
解决Oracle数据库用户密码过期
May 11 #Oracle
Oracle中DBLink的详细介绍
instantclient客户端 连接oracle数据库
清空 Oracle 安装记录并重新安装
SQL试题 使用窗口函数选出连续3天登录的用户
Oracle用户管理及赋权
Apr 24 #Oracle
分析SQL窗口函数之取值窗口函数
Apr 21 #Oracle
You might like
模拟xcopy的函数
2006/10/09 PHP
网页的标准,IMG不支持onload标签怎么办
2006/06/29 Javascript
JavaScript 快捷键设置实现代码
2009/03/13 Javascript
js在数组中删除重复的元素自保留一个(两种实现思路)
2014/08/22 Javascript
JavaScript中自定义事件用法分析
2014/12/23 Javascript
Javascript中使用parseInt函数需要注意的问题
2015/04/02 Javascript
js光标定位文本框回车表单提交问题的解决方法
2015/05/11 Javascript
浅谈js对象的创建和对6种继承模式的理解和遐想
2016/10/16 Javascript
NodeJS配置HTTPS服务实例分享
2017/02/19 NodeJs
vue父子组件的数据传递示例
2017/03/07 Javascript
jQuery实现Select下拉列表进行状态选择功能
2017/03/30 jQuery
Ajax异步文件上传与NodeJS express服务端处理
2017/04/01 NodeJs
简单实现js轮播图效果
2017/07/14 Javascript
在vue项目中使用Nprogress.js进度条的方法
2018/01/31 Javascript
详解使用element-ui table组件的筛选功能的一个小坑
2018/11/02 Javascript
vue项目从node8.x升级到12.x后的问题解决
2019/10/25 Javascript
Nuxt.js 静态资源和打包的操作
2020/11/06 Javascript
[36:33]Ti4 循环赛第四日 附加赛NEWBEE vs Mouz
2014/07/13 DOTA
Python中声明只包含一个元素的元组数据方法
2014/08/25 Python
python安装与使用redis的方法
2016/04/19 Python
Python中list查询及所需时间计算操作示例
2018/06/21 Python
pywinauto自动化操作记事本
2019/08/26 Python
CSS3 实现的火焰动画
2020/12/07 HTML / CSS
马来西亚最大的在线隐形眼镜商店:MrLens
2019/03/27 全球购物
环境科学专业个人求职信
2013/12/15 职场文书
关于人生的感言
2014/01/17 职场文书
回门宴父母答谢词
2014/01/26 职场文书
信息技术毕业生自荐信范文
2014/03/13 职场文书
民主评议党员工作总结
2014/10/20 职场文书
教师群众路线教育实践活动个人对照检查材料
2014/11/04 职场文书
工作态度恶劣检讨书
2015/05/06 职场文书
电影雷锋观后感
2015/06/10 职场文书
幼儿园庆元旦主持词
2015/07/06 职场文书
2016年幼儿园教师政治学习心得体会
2016/01/23 职场文书
2019年恭贺升学祝福语集锦
2019/08/15 职场文书
Python通用验证码识别OCR库ddddocr的安装使用教程
2022/07/07 Python