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 数据仓库ETL技术之多表插入语句的示例详解
Apr 12 Oracle
Oracle设置DB、监听和EM开机启动的方法
Apr 25 Oracle
使用springboot暴露oracle数据接口的问题
May 07 Oracle
使用Oracle跟踪文件的问题详解
Jun 28 Oracle
oracle索引总结
Sep 25 Oracle
Lakehouse数据湖并发控制陷阱分析
Mar 31 Oracle
详解SQL的窗口函数
Apr 21 Oracle
分析SQL窗口函数之取值窗口函数
Apr 21 Oracle
清空 Oracle 安装记录并重新安装
Apr 26 Oracle
解决Oracle数据库用户密码过期
May 11 Oracle
Oracle锁表解决方法的详细记录
Jun 05 Oracle
解决Oracle数据库用户密码过期
May 11 #Oracle
Oracle中DBLink的详细介绍
instantclient客户端 连接oracle数据库
清空 Oracle 安装记录并重新安装
SQL试题 使用窗口函数选出连续3天登录的用户
Oracle用户管理及赋权
Apr 24 #Oracle
分析SQL窗口函数之取值窗口函数
Apr 21 #Oracle
You might like
ThinkPHP2.0读取MSSQL提示Incorrect syntax near the keyword 'AS'的解决方法
2014/06/25 PHP
PHP编译安装中遇到的两个错误和解决方法
2014/08/20 PHP
PHP实现图片上传并压缩
2015/12/22 PHP
javascript IE中的DOM ready应用技巧
2008/07/23 Javascript
javascript 表格排序和表头浮动效果(扩展SortTable)
2009/04/07 Javascript
JQuery学习笔记 nt-child的使用
2011/01/17 Javascript
javascript的回调函数应用示例
2014/02/20 Javascript
jQuery的选择器中的通配符使用介绍
2014/03/20 Javascript
js图片处理示例代码
2014/05/12 Javascript
使用JQuery 加载页面时调用JS的实现方法
2016/05/30 Javascript
浅谈JS封闭函数、闭包、内置对象
2017/07/18 Javascript
vue实现手机号码抽奖上下滚动动画示例
2017/10/18 Javascript
vue select二级联动第二级默认选中第一个option值的实例
2018/01/10 Javascript
微信小程序实现带放大效果的轮播图
2020/05/26 Javascript
python集合类型用法分析
2015/04/08 Python
python同时给两个收件人发送邮件的方法
2015/04/30 Python
python实现简单中文词频统计示例
2017/11/08 Python
分享一下Python数据分析常用的8款工具
2018/04/29 Python
numpy实现合并多维矩阵、list的扩展方法
2018/05/08 Python
让你Python到很爽的加速递归函数的装饰器
2019/05/26 Python
利用pyuic5将ui文件转换为py文件的方法
2019/06/19 Python
Python程序打包工具py2exe和PyInstaller详解
2019/06/28 Python
python处理大日志文件
2019/07/23 Python
pandas中read_csv的缺失值处理方式
2019/12/19 Python
Python异步编程之协程任务的调度操作实例分析
2020/02/01 Python
Python Opencv实现单目标检测的示例代码
2020/09/08 Python
python判断all函数输出结果是否为true的方法
2020/12/03 Python
C++:memset ,memcpy和strcpy的根本区别
2013/04/27 面试题
静态变量和实例变量的区别
2015/07/07 面试题
采购员的工作职责
2013/12/26 职场文书
师范院校学生自荐信范文
2013/12/27 职场文书
《白鹅》教学反思
2014/04/13 职场文书
夏季药店促销方案
2014/08/22 职场文书
银行员工犯错检讨书
2014/09/16 职场文书
MySQL普通表如何转换成分区表
2022/05/30 MySQL
二维码条形码生成的JavaScript脚本库
2022/07/07 Javascript