服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

Mysql|Sql Server|Oracle|Redis|MongoDB|PostgreSQL|Sqlite|DB2|mariadb|Access|数据库技术|

服务器之家 - 数据库 - Oracle - Oracle去除重复数据

Oracle去除重复数据

2022-09-08 18:14springsnow Oracle

这篇文章介绍了Oracle去除重复数据的方法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

查询某些字段相同的记录

如:查询col1与col2值相同的记录:

?
1
select a.* from table1 a, table1 b where a.id <> b.id and a.col1 = b.col1 and a.col2 = b.col2;

一、用rowid方法:

根据oracle自带的rowid属性进行判断是否存在重复记录。

rowid伪列用于唯一标识物理位置的表行,当用insert插入数据时,会自动生成rowid,与数据一起存放,形如:AAAL=XAAAEAAAAA。

1、查数据:

?
1
2
select * from    table1 a where rowid!=
(select max(rowid) from table1 b where   a.col1 = b.col1 and a.col2 = b.col2;

2、删数据:

保留rowid最大的记录:

?
1
2
delete  from    table1 a where rowid!=
(select max(rowid) from table1 b where   a.col1 = b.col1 and a.col2 = b.col2;

二、group by 方法:

1、查数据:

?
1
2
select * from    table1 a where (a.col1,a.col2) in
(select col1,col2 from  table1 group by  col1,col2 having count(*)>1)

2、删数据:

删除表中多余的重复记录(多个字段),只保留rowid最小的记录。

?
1
2
3
4
delete  from    table1 a where (a.col1,a.col2) in
(select col1,col2 from  table1 group by  col1,col2 having count(*)>1)
 and rowid not in
(select min(rowid) from  table1 group by  col1,col2 having count(*)>1)

到此这篇关于Oracle去除重复数据的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://www.cnblogs.com/springsnow/p/9394906.html

延伸 · 阅读

精彩推荐