譬喻:
id           name         value
1               a                 pp
2               a                 pp
3               b                 iii
4               b                 pp
5               b                 pp
6               c                 pp
7               c                 pp
8               c                 iii
id是主键
哀求 得到如许的结果
id           name         value
1               a                 pp
3               b                 iii
4               b                 pp
6               c                 pp
8               c                 iii

行动 1
delete   YourTable
where   [id]   not   in   (
select   max([id])   from   YourTable
group   by   (name   +   value))

行动 2
delete   a
from   表   a   left   join(
select   id=min(id)   from   表   group   by   name,value
)b   on   a.id=b.id
where   b.id   is   null

查询及删除频频记实的SQL语句
查询及删除频频记实的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
)

Leave a Reply

电子邮件地址不会被公开。 必填项已用*标注