工作中,发现Oracle数据库表中有许多重复的数据,而这个时候老板需要统计表中有多少条数据时(不包含重复数据),只想说一句MMP,库中好几十万数据,肿么办,无奈只能自己在网上找语句,最终成功解救,下面是我一个实验,很好理解。
假设有一张人员信息表cs(姓名,证件号,地址),将表中三个字段数据都重复的数据筛选出来:
distinct:这个关键字来过滤掉多余的重复数据只保留一条数据
select * from from cs ------所有字段
select distinct xm,zjh,dz from cs; -----指定字段
在实践中往往只用它来返回不重复数据的条数,因为distinct对于一个数据量非常大的库来说,无疑是会直接影响到效率的。
-----------------------------------------------------------------------------------------------------------------------
查询重复数据、删除重复数据的方法如下:↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓
①rowid用法: oracle带的rowid属性,进行判断是否存在重复数据。
查询重复数据:
select a.* from cs a where rowid !=(select max(rowid) from cs b where a.xm=b.xm and a.zjh=b.zjh and a.dz=b.dz)
删除重复数据:
delete from cs a where rowid !=(select max(rowid) from cs b where a.xm=b.xm and a.zjh=b.zjh and a.dz=b.dz)
②group by :一般用于将查询结果分组,多配合聚合函数,sum,count,min,max,having等一起使用。
查询重复数据:
select max(xm),max(zjh),max(dz),count(xm) as 记录数 from cs group by xm having count(xm)>1 ---------适用于字段少的
select * from cs a where (a.xm,a.zjh,a.dz) in (select xm,zjh,dz from cs group by xm,zjh,dz having count(*)>1)
and rowid not in (select min(rowid) from cs group by xm,zjh,dz having count(*)>1) -------适用于多字段
去重重复数据:多个字段,只留有rowid最小的记录 。
delete from cs a where (a.xm,a.zjh,a.dz) in (select xm,zjh,dz from cs group by xm,zjh,dz having count(*)>1) and rowid not in (select min(rowid) from cs group by xm,zjh,dz having count(*)>1)
③row_number()over(partition by 列)
select xm,zjh,dz,row_number()over(partition by zjh order by xm) 记录号 from cs
去重重复数据:
with cs1 as (select xm,zjh,dz,row_number()over(partition by zjh order by zjh) 记录号 from cs)select * from cs1 where 记录号=1
更多Oracle相关信息见 Oracle 专题页面 https://www.linuxidc.com/topicnews.aspx?tid=12
Linux公社的RSS地址 : https://www.linuxidc.com/rssFeed.aspx
本文永久更新链接地址: https://www.linuxidc.com/Linux/2018-09/15434.htm
以上所述就是小编给大家介绍的《Oracle数据库查询重复数据及删除重复数据方法》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:- kafka 删除 topic 及数据
- SQLServer之删除数据库架构
- 在微服务架构下分散数据的删除
- 记一次大批量物理删除数据
- FLEX ArrayCollection删除过滤的数据问题解决
- Vue实现动态创建和删除数据的方法
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Responsive Web Design
Ethan Marcotte / Happy Cog / 2011-6 / USD 18.00
From mobile browsers to netbooks and tablets, users are visiting your sites from an increasing array of devices and browsers. Are your designs ready? Learn how to think beyond the desktop and craft be......一起来看看 《Responsive Web Design》 这本书的介绍吧!
在线进制转换器
各进制数互转换器
HSV CMYK 转换工具
HSV CMYK互换工具