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通过存储过程上传list保存功能
May 12 Oracle
RPM包方式安装Oracle21c的方法详解
Aug 23 Oracle
Oracle 死锁的检测查询及处理
Sep 25 Oracle
oracle索引总结
Sep 25 Oracle
排查并解决Oracle sysaux表空间异常增长
Apr 20 Oracle
Oracle用户管理及赋权
Apr 24 Oracle
instantclient客户端 连接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
php中常用字符串处理代码片段整理
2011/11/07 PHP
php函数实现判断是否移动端访问
2015/03/03 PHP
php识别翻转iphone拍摄的颠倒图片
2018/05/17 PHP
PHP实现微信申请退款功能
2018/10/01 PHP
网页和浏览器兼容性问题汇总(draft1)
2009/06/01 Javascript
js 有框架页面跳转(target)三种情况下的应用
2013/04/09 Javascript
在jQuery中 关于json空对象筛选替换
2013/04/15 Javascript
JavaScript实现数据类型的相互转换
2016/03/06 Javascript
第九章之路径分页标签与徽章组件
2016/04/25 Javascript
js仿百度切换皮肤功能(html+css)
2016/07/10 Javascript
jQuery实现的导航下拉菜单效果示例
2016/09/05 Javascript
JavaScript & jQuery完美判断图片是否加载完毕
2017/01/08 Javascript
js实现文本上下来回滚动
2017/02/03 Javascript
Nodejs 发送Post请求功能(发短信验证码例子)
2017/02/09 NodeJs
微信小程序 页面跳转如何实现传值
2017/04/05 Javascript
vue-cli项目修改文件热重载失效的解决方法
2018/09/19 Javascript
微信小程序里引入SVG矢量图标的方法
2019/09/20 Javascript
[22:20]初生之犊-TI4第5名LGD战队纪录片
2014/08/13 DOTA
[49:41]NB vs NAVI Supermajor小组赛A组 BO3 第一场 6.2
2018/06/03 DOTA
Python日期操作学习笔记
2008/10/07 Python
Pyhthon中使用compileall模块编译源文件为pyc文件
2015/04/28 Python
详解使用python crontab设置linux定时任务
2016/12/08 Python
Python实现读取文件最后n行的方法
2017/02/23 Python
Django生成PDF文档显示在网页上以及解决PDF中文显示乱码的问题
2019/07/04 Python
flask实现验证码并验证功能
2019/12/05 Python
使用OpenCV校准鱼眼镜头的方法
2020/11/26 Python
学术诚信承诺书
2014/05/26 职场文书
政府法律服务方案
2014/06/14 职场文书
白酒营销策划方案
2014/08/17 职场文书
群众路线领导干部个人对照检查材料(集锦)
2014/09/23 职场文书
2014年幼儿园教研工作总结
2014/12/04 职场文书
2015年民主生活会发言材料
2014/12/15 职场文书
企业法律事务工作总结
2015/08/11 职场文书
银行客户经理培训心得体会
2016/01/09 职场文书
职业规划从高考志愿专业选择开始
2019/08/08 职场文书
手残删除python之后的补救方法
2021/06/26 Python