oracel 查询删除重复记录的几种方法
建表语句
CREATE TABLE Persons
(
PersonID int,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255)
);
insert into Persons values(1,'a','aa','aaa','aaaa');
insert into Persons values(1,'a','aa','aaa','aaaa');
insert into Persons values(1,'a','aa','aaa','aaaa');
insert into Persons values(1,'a','aa','aaa','aaaa');
insert into Persons values(2,'a','aa','aaa','aaaa');
insert into Persons values(2,'a','aa','aaa','aaaa');
insert into Persons values(3,'a','aa','aaa','aaaa');
insert into Persons values(3,'a','aa','aaa','aaaa');
insert into Persons values(4,'a','aa','aaa','aaaa');
insert into Persons values(5,'a','aa','aaa','aaaa');
select * from Persons order by PersonID;
PERSONID LASTNAME FIRSTNAME ADDRESS CITY
1 1 a aa aaa aaaa
2 1 a aa aaa aaaa
3 1 a aa aaa aaaa
4 1 a aa aaa aaaa
5 2 a aa aaa aaaa
6 2 a aa aaa aaaa
7 3 a aa aaa aaaa
8 3 a aa aaa aaaa
9 4 a aa aaa aaaa
10 5 a aa aaa aaaa
1、查找表中多余的重复记录,重复记录是根据单个字段(PersonID)来判断。
select * from Persons where PersonID in ( select PersonID from Persons group by PersonID having(count(PersonID))>1);
order by PersonID;
PERSONID LASTNAME FIRSTNAME ADDRESS CITY
1 a aa aaa aaaa
1 a aa aaa aaaa
1 a aa aaa aaaa
1 a aa aaa aaaa
2 a aa aaa aaaa
2 a aa aaa aaaa
3 a aa aaa aaaa
3 a aa aaa aaaa
2、删除表中多余的重复记录,重复记录是根据单个字段(PersonID)来判断,只留有rowid最小的记录。
select * from Persons
where PersonID in (select PersonID from Persons group by PersonID having count(PersonID) > 1)
and rowid not in (select min(rowid) from Persons group by PersonID having count(PersonID )>1)
order by PersonID;
delete from Persons
where PersonID in (select PersonID from Persons group by PersonID having count(PersonID) > 1)
and rowid not in (select min(rowid) from Persons group by PersonID having count(PersonID )>1)
PERSONID LASTNAME FIRSTNAME ADDRESS CITY
1 a aa aaa aaaa
1 a aa aaa aaaa
1 a aa aaa aaaa
2 a aa aaa aaaa
3 a aa aaa aaaa
3、查找表中多余的重复记录(多个字段)
select * from Persons a
where (a.PersonID,a.City) in (select PersonID,City from Persons group by PersonID,City having count(*) > 1)
order by PersonID;
PERSONID LASTNAME FIRSTNAME ADDRESS CITY
1 a aa aaa aaaa
1 a aa aaa aaaa
1 a aa aaa aaaa
1 a aa aaa aaaa
2 a aa aaa aaaa
2 a aa aaa aaaa
3 a aa aaa aaaa
3 a aa aaa aaaa
4、删除表中多余的重复记录(多个字段),只留有rowid最小的记录
select * from Persons a
where (a.PersonID,a.City) in (select PersonID,City from Persons group by PersonID,City having count(*) > 1)
and rowid not in (select min(rowid) from Persons group by PersonID,City having count(*)>1)
order by PersonID;
delete from Persons a
where (a.PersonID,a.City) in (select PersonID,City from Persons group by PersonID,City having count(*) > 1)
and rowid not in (select min(rowid) from Persons group by PersonID,City having count(*)>1)
PERSONID LASTNAME FIRSTNAME ADDRESS CITY
1 a aa aaa aaaa
1 a aa aaa aaaa
1 a aa aaa aaaa
2 a aa aaa aaaa
3 a aa aaa aaaa
5、查找表中多余的重复记录(多个字段),不包含rowid最小的记录
select * from Persons a
where (a.PersonID,a.City) in (select PersonID,City from Persons group by PersonID,City having count(*) > 1)
and rowid not in (select min(rowid) from Persons group by PersonID,City having count(*)>1)
and rowid not in (select min(rowid) from Persons group by PersonID,seq having count(*)>1)
order by PersonID;
PERSONID LASTNAME FIRSTNAME ADDRESS CITY
1 a aa aaa aaaa
1 a aa aaa aaaa
1 a aa aaa aaaa
2 a aa aaa aaaa
3 a aa aaa aaaa
oracel 查询删除重复记录的几种方法的更多相关文章
- oracle中查找和删除重复记录的几种方法总结
平时工作中可能会遇到当试图对库表中的某一列或几列创建唯一索引时,系统提示 ORA-01452 :不能创建唯一索引,发现重复记录. 下面总结一下几种查找和删除重复记录的方法(以表CZ为例): 表CZ的结 ...
- spring+hibernate删除单条记录的几种方法
spring+hibernate删除单条记录的几种方法
- ORACLE查询删除重复记录三种方法
本文列举了3种删除重复记录的方法,分别是rowid.group by和distinct,小伙伴们可以参考一下. 比如现在有一人员表 (表名:peosons) 若想将姓名.身份证号.住址这三个字段完 ...
- ORACLE查询删除重复记录
比如现在有一人员表 (表名:peosons) 若想将姓名.身份证号.住址这三个字段完全相同的记录查询出来 复制代码 代码如下: select p1.* from persons p1,pers ...
- Oracle中查询和删除相同记录的3种方法
--创建测试表 )); ,'); ,'); ,'); ,'); ,'); ,'); commit; select * from test; --查询相同记录 ); select id,name fro ...
- SQL Server 数据库查找重复记录的几种方法
http://www.hanyu123.cn/html/c61/6790.html 一.查某一列(或多列)的重复值.(只可以查出重复记录的值,不能查出整个记录的信息) 例如:查找stuid,stuna ...
- python列表删除重复元素的三种方法
给定一个列表,要求删除列表中重复元素. listA = ['python','语','言','是','一','门','动','态','语','言'] 方法1,对列表调用排序,从末尾依次比较相邻两个元素 ...
- 在MS SQL删除重复行的几种方法
1.如果有ID字段,就是具有唯一性的字段 delect table where id not in ( select max(id) ...
- Oracle 删除重复数据的几种方法
去重 第一种:distinct create table tmp_t3 as select distinct * from t3; drop table t3; alter table tmp_t2 ...
随机推荐
- globalalloc、malloc和new的区别
简单来说: malloc是c分配内存的库函数,new是c++分配内存的操作符,而globalalloc是win32提供的分配内存的API malloc不能自动调用构造和析构函数,在c++中没什么实用价 ...
- WPF实现Twitter按钮效果(转)
最近上网看到这个CSS3实现的Twitter按钮,感觉很漂亮,于是想用WPF来实现下. 实现这个效果,参考了CSS3 原文地址:http://www.html5tricks.com/css3-twit ...
- new/delete和malloc/free区别
相同点: 都可用于申请动态内存和释放内存 不同点: 操作对象有所不同. 本质区别: malloc与free是C++/C 语言的标准库函数,new/delete 是C++的运算符,对象在创建的同时要自动 ...
- Dev-C++ 小问题锦集
C++ project cann't debug Your project does not have debugging information, do you want to enable deb ...
- Linux VPS禁止某个IP访问
http://www.vpser.net/security/linux-vps-deny-ip.html
- 使用Apache POI操作Excel文件---在已有的Excel文件中插入一行新的数据
package org.test; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundEx ...
- ThreadPoolExecutor之三:自定义线程池-扩展示例
ThreadPoolExecutor是可扩展的,下面一个示例: package com.dxz.threadpool.demo1; import java.util.concurrent.Blocki ...
- http遇到的那些坑,iis上传文件报413错误 asp.net MVC
话不多说,直接上解决方法. 修改配置文件 applicationHost.config 具体地址在C:\Windows\System32\inetsrv\config 按照下面的节点进行 添加&l ...
- 5月8日上课笔记-浮动float
IO文件复制 字符流(只能对文本文件进行操作) Reader Writer 字节流(对所有文件都能操作) InputStream OutputStream 一.浮动 边框弧度 border-radiu ...
- kafka常用命令(cdh5.10.0+kafka)
参考资料:http://kafka.apache.org/quickstart 进入kafka安装目录(CDH安装路径为:/opt/cloudera/parcels/KAFKA):进入bin目录: c ...