SQL重复记录查询方法

2008年08月14日 星期四 21:01

SQL重复记录查询

1、查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断
select * from people
where peopleId in (select   peopleId from   people group by   peopleId having count

(peopleId) > 1)

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

(peopleId) > 1)
and rowid not in (select min(rowid) from   people group by peopleId having count(peopleId

)>1)

3、查找表中多余的重复记录(多个字段) 
select * from vitae a
where (a.peopleId,a.seq) in   (select peopleId,seq from vitae group by peopleId,seq having

count(*) > 1)

4、删除表中多余的重复记录(多个字段),只留有rowid最小的记录
delete from vitae a
where (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 a
where (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)
(二)
比方说
在A表中存在一个字段“name”,
而且不同记录之间的“name”值有可能会相同,
现在就是需要查询出在该表中的各记录之间,“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 integer
declare cur_rows cursor local for select 主字段,count(*) from 表名 group by 主字段 having

count(*) >; 1
open cur_rows
fetch cur_rows into @id,@max
while @@fetch_status=0
begin
select @max = @max -1
set rowcount @max
delete from 表名 where 主字段 = @id
fetch cur_rows into @id,@max
end
close cur_rows
set rowcount 0

  方法二

  有两个意义上的重复记录,一是完全重复的记录,也即所有字段均重复的记录,二是部分关键字段重

复的记录,比如Name字段重复,而其他字段不一定重复或都重复可以忽略。

  1、对于第一种重复,比较容易解决,使用
select distinct * from tableName

  就可以得到无重复记录的结果集。

  如果该表需要删除重复的记录(重复记录保留1条),可以按以下方法删除
select distinct * into #Tmp from tableName
drop table tableName
select * into tableName from #Tmp
drop table #Tmp

  发生这种重复的原因是表设计不周产生的,增加唯一索引列即可解决。

  2、这类重复问题通常要求保留重复记录中的第一条记录,操作方法如下

  假设有重复的字段为Name,Address,要求得到这两个字段唯一的结果集
select identity(int,1,1) as autoID, * into #Tmp from tableName
select min(autoID) as autoID into #Tmp2 from #Tmp group by Name,autoID
select * from #Tmp where autoID in(select autoID from #tmp2)

  最后一个select即得到了Name,Address不重复的结果集(但多了一个autoID字段,实际写时可以写

在select子句中省去此列)

(四)查询重复
select * from tablename where id in (
select id from tablename 
group by id 
having count(id) > 1
)

oracle查询重复数据方法的更多相关文章

  1. Oracle 查询重复数据方法

    查询某个字段存在重复数据的方法: select * from tablename where id in (select id from tablename group by id having co ...

  2. oracle 查询重复数据并且删除, 只保留一条数据

    数据库操作中,经常会因为导数据造成数据重复,需要进行数据清理,去掉冗余的数据,只保留正确的数据 一:重复数据根据单个字段进行判断 1.首先,查询表中多余的数据,由关键字段(name)来查询. sele ...

  3. oracle查询重复数据出现次数

    话不多数上代码: 我在Oracle数据库查数据,发现重复数据,于是我想把重复条数以及具体数据查出来: 下面是数据 然后我需要知道重复多少条 (重复十条,也就是有五条数据相同) SQL: select ...

  4. oracle 查询重复数据并且删除, 只保留一条数据重复数据

    最近面试中都遇到了这样一个数据库题: 删除表中的重复数据,有且只保留一条重复数据. 思路: 1)这个题需要用到rowid,首先找到重复数据的rowid,并找出rowid最大或最小值,作为删除的条件: ...

  5. Oracle查询重复数据并删除,只保留一条记录

    前言 项目中,在“资源目录-在线编目”中,资源项子表存在多条重发数据,需要进行数据清理,删除重发的数据,最终只保留一条相同的数据. 操作的表名:R_RESOURCE_DETAILS 操作步骤 一.重复 ...

  6. 笔记:Oracle查询重复数据并删除,只保留一条记录

    1.查找表中多余的重复记录,重复记录是根据单个字段(Id)来判断 select * from 表 where Id in (select Id from 表 group byId having cou ...

  7. oracle 查询重复数据

    SELECT * FROM td_attrval_group WHERE parent_attrval_id IN(SELECT parent_attrval_id FROM td_attrval_g ...

  8. Oracle查询表里的重复数据方法:

    一.背景 一张person表,有id和name的两个字段,id是唯一的不允许重复,id相同则认为是重复的记录. 二.解决 select id from group by id having count ...

  9. Oracle查询表里的重复数据方法

    select id from group by id having count(*) > 1 按照id分组并计数,某个id号那一组的数量超过1条则认为重复. 如何查询重复的数据 select 字 ...

随机推荐

  1. C代码快速构建框架

    #include "stdio.h" typedef char int8_t; typedef short int16_t; typedef int int32_t; typede ...

  2. luogu4932 浏览器 (拆)

    分析1的个数的奇偶性: 奇xor奇=偶xor偶=偶 奇xor偶=奇 所以只要统计1的个数是奇数的数的个数 和 是偶数的个数 乘一起就行了 直接用bitset来做,虽然常数很小/数据随机可以过,但复杂度 ...

  3. A1033. To Fill or Not to Fill

    With highways available, driving a car from Hangzhou to any other city is easy. But since the tank c ...

  4. C# winform C/S WebBrowser 微信第三方登录

    网上很多的资料都是B/S结构的,这里是基于C# C/S 结构的微信第三方授权登录 一.准备知识 1 http Get和Post方法.做第三方授权登录,获取信息基本上都是用get和post方法,做之前需 ...

  5. java: 关于从jar中读取资源遇到的问题getClass().getResource(...)

    在Java的程序发布中,很多人会选择采用二进制的jar的格式进行发布,怎么样读取Jar里面的资源呢?主要是采用ClassLoader的下面几个方法来实现:public URL getResource( ...

  6. 收藏:FLASH中键检测与右键屏蔽

    原文:http://space.flash8.net/space/?591172/viewspace-708726.html <!DOCTYPE html PUBLIC "-//W3C ...

  7. 2018 ACM 网络选拔赛 徐州赛区

    A. Hard to prepare #include <cstdio> #include <cstdlib> #include <cmath> #include ...

  8. Kafka配置文件server.properties(三个版本)

    前言 其实每个版本都有些许改动,只不过改动大小而已,但是网上的教程都真的太老了,其实更新一下也费不了多少时间 0.9.0 # Licensed to the Apache Software Found ...

  9. request请求地址

    1.String contextPath = httpServletRequest.getServletContext().getContextPath(); /项目名称 2.String conte ...

  10. 基于windows server 2012 的微软桌面虚拟化实战教程

    http://abool.blog.51cto.com/8355508/1587489/ Windows Server2012 中的“远程桌面服务”服务器角色中就提供了允许用户连接到虚拟机.Remot ...