日期:2014-05-18  浏览次数:20676 次

SQL去重复查询
有一张Access的查询表,结果如下.
C/C++ code

name    Item    value    tag
John    ITCH    89.33    1
John    ITCH    33.22    2
Alex    ITCH    67.77    3
Jacky    ITCH    89.01    1
Alex    YHTC    34.22    2
Bruce    YHTC    35.11    2
Bruce    YHTC    89.11    3
...


这张表有一个缺憾,在Item ITCH上,John有两笔记录(1/2),同样,在YHTC项目,Bruce也有两笔记录(2/3).这是不符合规则的.
目前需求是,对于同一个项目,每人只能取第一笔数据.也就是John tag2和Bruce tag3是不合法的,需要删除.
问题是,这张表已经是一张查询表了,那么该怎么编写SQL才能从这张表中删除不需要的数据?
注意,这是access的环境,oracle的一些SQL语句是失效的.

------解决方案--------------------
SQL code
--功能概述:删除重复记录 

在几千条记录里,存在着些相同的记录,如何能用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

------解决方案--------------------
delete a
from ta a
where exists(select 1 from ta where name=a.name amd Item = a.Item and flag <a.flag)
------解决方案--------------------
SQL code
select * from tb t 
where not exists(select * from tb where t.name=name and t.item=item and tag<t.tag )

------解决方案--------------------
SQL code
delete a 
from ta a 
where tag in (select min(tag)  from ta where name=a.name amd Item = a.Item  )

------解决方案--------------------
SQL code
delete a 
from ta a 
where tag <> (select min(tag)  from ta where name=a.name amd Item = a.Item  )

------解决方案--------------------
SQL code
---------------------------------
--  Author: HEROWANG(让你望见影子的墙)
--  Date  : 2009-07-15 07:06:42
---------------------------------
 
IF OBJECT_ID('[tb]') IS NOT NULL 
    DROP TABLE [tb]
go
CREATE TABLE [tb] (name VARCHAR(5),Item VARCHAR(4),value NUMERIC(4,2),tag INT)
INSERT INTO [tb]
SELECT 'John','ITCH',89.33,1 UNION ALL
SELECT 'John','ITCH',33.22,2 UNION ALL
SELECT 'Alex','ITCH',67.77,3 UNION ALL
SELECT 'Jacky','ITCH',89.01,1 UNION ALL
SELECT 'Alex','YHTC',34.22,2 UNION ALL
SELECT 'Bruce','YHTC',35.11,2 UNION ALL
SELECT 'Bruce','YHTC',89.11,3



delete t
from tb t where  exists(select 1 from t