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 ...
随机推荐
- 使用C#和Java发送邮件(转载)
using System.Net.Mail; using System.Net; public class EmailEntity { private MailMessage mm; /// < ...
- MYSQL数据库设计之字段选择原则
关于字段的选择其实很多地方都有进行详细的介绍,我这里只写一下我在使用过程中的心得感受.如果想要全面的了解的话,大家可以去看高性能MYSQL这一本书籍,里面有一章节介绍的特别全面,基本涉及MYSQL中全 ...
- Java 设计模式之工厂模式(二)
原文地址:Java 设计模式之工厂模式(二) 博客地址:http://www.extlight.com 一.背景 本篇内容是 Java 设计模式创建型模式的第二篇.上一篇主题为 <Java 设计 ...
- RK3288 指令查看HDMI当前分辨率和支持的分辨率
$ adb shell root@xxx:/ # cd /sys/class/display/HDMI cd /sys/class/display/HDMI root@xxx:/sys/class/d ...
- 【转】JMeter使用指南
Abstract 本文重点介绍JMeter工具在测试中地位以及其中一些难以理解或者手册中含糊不清的感念,读者可以通过本文了解这些概念,然后再根据自己的需要查阅JMeter中各个组件的具体用法来完成测试 ...
- 阻塞队列之一:BlockingQueue汇总
一.阻塞队列介绍 BlockingQueue 通常用于一个线程生产对象,而另外一个线程消费这些对象的场景.下图是对这个原理的阐述: 一个线程往里边放,另外一个线程从里边取的一个 BlockingQue ...
- Java-Runoob:Java 日期时间
ylbtech-Java-Runoob:Java 日期时间 1.返回顶部 1. Java 日期时间 java.util 包提供了 Date 类来封装当前的日期和时间. Date 类提供两个构造函数来实 ...
- pyc是什么
python是解释型语言,需要解释器对程序逐行做出解释,然后直接运行. C语言是编译型语言,PC不需要翻译,直接执行就可以了. java也是解释型语言,不过速度可以跟编译型媲美. 用java举例,ja ...
- 运维平台cmdb开发-day2
一 发送数据到api(Django的URL) 发送请求携带参数 requests.get(url='http://127.0.0.1:8000/api/asset/?k1=123') # <Qu ...
- js获取页面名称和路径参数
// 取当前页面名称(不带后缀名)function getPageName1(){ var a = location.href; var b = a.split("/" ...