SQL查询重复数据和清除重复数据

上传人:ba****u 文档编号:184016352 上传时间:2023-02-01 格式:DOCX 页数:6 大小:17.71KB
收藏 版权申诉 举报 下载
SQL查询重复数据和清除重复数据_第1页
第1页 / 共6页
SQL查询重复数据和清除重复数据_第2页
第2页 / 共6页
SQL查询重复数据和清除重复数据_第3页
第3页 / 共6页
资源描述:

《SQL查询重复数据和清除重复数据》由会员分享,可在线阅读,更多相关《SQL查询重复数据和清除重复数据(6页珍藏版)》请在装配图网上搜索。

1、SQL查询重复数据和清除重复数据分类:SQL 2008-05-20 11:03 34086人阅读评论(1)收藏举报sqlsqlserversunjoin选择重复,消除重复和选择出序列有例表:empemp_nonameage001Tom17002Sun14003Tom15004Tom16要求:列出所有名字重复的人的记录(1) 最直观的思路:要知道所有名字有重复人资料,首先必须知道哪个名字重复了:select name from emp group by name having count(*)1所有名字重复人的记录是:select * from empwhere name in (select

2、name from emp group by name having count(*)1)(2) 稍微再聪明一点,就会想到,如果对每个名字都和原表进行比较,大于2个人名字与这 条记录相同的就是合格的,就有select * from emp where (select count(*) from emp e where e.name=emp.name) 1注意一下这个1,想下如果是=1,如果是=2如果是2如果e是另外一张表而且是=0 那结果 就更好玩了:)这个过程是在判断工号为001的人的时候先取得001的名字(emp.name)然后和原表 的名字进行比较e.name注意e是emp的一个别名。再

3、稍微想得多一点,就会想到,如果有另外一个名字相同的人工号不与她他相同那么这条记 录符合要求:select * from empwhere exists(select * from emp e where e.name=emp.name and e.emp_noemp.emp_no)此思路的join写法:select emp.* from emp,emp ewhere emp.name=e.name and emp.emp_noe.emp_no/* 这个语句较规范的 join 写法是select emp.* from emp inner join emp eon emp.name=e.name

4、andemp.emp_noe.emp_no但个人比较倾向于前一种写法,关键是更清晰*/b、有例表:empage16nameTomSun14Tom16Tom16清除重复过滤掉所有多余的重复记录(1) 我们知道distinct、group by可以过滤重复,于是就有最直观的select distinct * from emp 或 select name,age from emp group by name,age获得需要的数据,如果可以使用临时表就有解法:select distinct* into #tmpfromempdelete from empinsert intoemp select *f

5、rom#tmp(2) 但是如果不可以使用临时表,那该怎么办?我们观察到我们没办法区分数据(物理位置不一样,对SQL Server来说没有任何区别),思 路自然是想办法把数据区分出来了,既然现在的所有的列都没办法区分数据,唯一的办法就 是再加个列让它区分出来,加什么列好?最佳选择是identity列:altertableempadd chk int identity(1,1)表示例:nameagechkTom161Sun142Tom163Tom164重复记录可以表示为:select * from emp where (select count(*) from emp e where e.name

6、=emp.name)1要删除的是:delete from empwhere (select count(*) from emp e where e.name=emp.name and e.chk=emp.chk)1再把添加的列删掉,出现结果。alter table emp drop column chk(3) 另一个思路:视图select min(chk) from emp group by name having count(*) 1获得有重复的记录chk最小的值,于是可以delete from emp where chk not in (select min(chk) from emp g

7、roup by name) 写成join的形式也可以:有例表:empemp_nonameage001Tom17002Sun14003Tom15004Tom 16要求生成序列号(1) 最简单的方法,根据b问题的解法:alter table emp add chk int identity(1,1) 或select *,identity(int,1,1) chk into #tmp from emp如果需要控制顺序怎么办?select top 100000 *,identity(int,1,1) chk into #tmp from emp order by age(2) 假如不可以更改表结构,怎

8、么办?如果不可以唯一区分每条记录是没有办法的,在可以唯一区分每条记录的时候,可以使用a 中的count的思路解决这个问题select emp.*,(select count(*) from emp e where e.emp_no=emp.emp_no)from emporder by (select count(*) from emp e where e.emp_no 1)ORDER BY Title DESC一、查找重复记录1、查找全部重复记录Select * From表 Where重复字段In (Select重复字段 From表 Group By重复字段Having Count(*)1)

9、2、过滤重复记录(只显示一条)Select * From HZT Where ID In (Select Max(ID) From HZT Group By Title)注:此处显示ID最大一条记录二。删除重复记录1、删除全部重复记录(慎用)Delete 表 Where 重复字段 In (Select 重复字段 From 表 Group By 重复字段 Having Count(*)1)2、保留一条(这个应该是大多数人所需要的人_人)Delete HZT Where ID Not In (Select Max(ID) From HZT Group By Title)注:此处保留ID最大一条记录

10、1、查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断select * from peoplewhere peopleId in (select peopleId from people group by peopleId having count(peopleId) 1)2、删除表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断,只留有rowid 最小的记录delete from peoplewhere peopleld in (select peopleld from people group by peopleld having count(peo

11、pleld) 1) and rowid not in (select min(rowid) from people group by peopleId having count(peopleId )1)3、查找表中多余的重复记录(多个字段)select * from vitae awhere (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) 1)4、删除表中多余的重复记录(多个字段),只留有rowid最小的记录delete from vitae awhere (

12、a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) 1)and rowid not in (select min(rowid) from vitae group by peopleId,seq having count(*)1)5、查找表中多余的重复记录(多个字段),不包含rowid最小的记录select * from vitae awhere (a.peopleId,a.seq) in (select peopleId,seq from vitae group b

13、y peopleId,seq having count(*) 1)and rowid not in (select min(rowid) from vitae group by peopleId,seq having count(*)1)补充:有两个以上的重复记录,一是完全重复的记录,也即所有字段均重复的记录,二是部分关键 字段重复的记录,比如Name字段重复,而其他字段不一定重复或都重复可以忽略。1、对于第一种重复,比较容易解决,使用select distinct * from tableName就可以得到无重复记录的结果集。如果该表需要删除重复的记录(重复记录保留1条),可以按以下方法删除

14、select distinct * into #Tmp from tableNamedrop table tableNameselect * into tableName from #Tmpdrop table #Tmp发生这种重复的原因是表设计不周产生的,增加唯一索引列即可解决。2、这类重复问题通常要求保留重复记录中的第一条记录,操作方法如下假设有重复的字段为Name,Address,要求得到这两个字段唯一的结果集select identity(int,1,1) as autoID, * into #Tmp from tableNameselect min(autoID) as autoID

15、 into #Tmp2 from #Tmp group by Name,autoIDselect * from #Tmp where autoID in(select autoID from #tmp2)1、查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断复制代码代码如下:select * from peoplewhere peopleId in (select peopleId from people group by peopleId having count (peopleId) 1)2、删除表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断,只

16、留有rowid 最小的记录复制代码代码如下:delete from peoplewhere peopleld in (select peopleld from people group by peopleld having count (peopleld) 1)and rowid not in (select min(rowid) from people group by peopleId having count(peopleId )1)3、查找表中多余的重复记录(多个字段)复制代码代码如下:select * from vitae awhere (a.peopleId,a.seq) in (

17、select peopleId,seq from vitae group by peopleId,seq havingcount(*) 1)4、删除表中多余的重复记录(多个字段),只留有rowid最小的记录复制代码代码如下:delete from vitae awhere (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq havingcount(*) 1)and rowid not in (select min(rowid) from vitae group by peopleId,seq h

18、aving count(*)1)5、查找表中多余的重复记录(多个字段),不包含rowid最小的记录复制代码代码如下:select * from vitae awhere (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq havingcount(*) 1)and rowid not in (select min(rowid) from vitae group by peopleId,seq having count(*)1)(二)比方说在A表中存在一个字段“name”,而且不同记录之间的“nam

19、e”值有可能会相同,现在就是需要查询出在该表中的各记录之间,“name”值存在重复的项;复制代码代码如下:Select Name,Count(*) From A Group By Name Having Count(*) 1如果还查性别也相同大则如下:复制代码代码如下:Select Name,sex,Count(*) From A Group By Name,sex Having Count(*) 1(三)方法一复制代码代码如下:declare max integer,id integerdeclare cur_rows cursor local for select 主字段,count(*)

20、 from 表名 group by 主字段 havingcount(*) ;1open cur_rowsfetch cur_rows into id,maxwhile fetch_status=0beginselect max = max -1set rowcount maxdelete from 表名 where 主字段=idfetch cur_rows into id,maxendclose cur_rowsset rowcount 0方法二有两个意义上的重复记录,一是完全重复的记录,也即所有字段均重复的记录,二是部分 关键字段重复的记录,比如Name字段重复,而其他字段不一定重复或都重复

21、可以忽略。1、对于第一种重复,比较容易解决,使用复制代码代码如下:select distinct * from tableName就可以得到无重复记录的结果集。如果该表需要删除重复的记录(重复记录保留1条),可以按以下方法删除复制代码代码如下:select distinct * into #Tmp from tableNamedrop table tableNameselect * into tableName from #Tmpdrop table #Tmp发生这种重复的原因是表设计不周产生的,增加唯一索引列即可解决。2、这类重复问题通常要求保留重复记录中的第一条记录,操作方法如下假设有重复

22、的字段为Name,Address,要求得到这两个字段唯一的结果集复制代码代码如下:select identity(int,1,1) as autoID, * into #Tmp from tableNameselect min(autoID) as autoID into #Tmp2 from #Tmp group by Name,autoIDselect * from #Tmp where autoID in(select autoID from #tmp2)最后一个select即得到了 Name, Address不重复的结果集(但多了一个autoID字段,实际写时可以写在select子句中省去此列)(四)查询重复复制代码代码如下:select * from tablename where id in (select id from tablenamegroup by idhaving count(id) 1)

展开阅读全文
温馨提示:
1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
2: 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
3.本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
关于我们 - 网站声明 - 网站地图 - 资源地图 - 友情链接 - 网站客服 - 联系我们

copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!