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

删除表中多余的重复记录(多个字段),只留有id最小的记录的问题
1、查找表中多余的重复记录(多个字段) 
select * from vitae a
where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1)

此句运行出错,
服务器: 消息 170,级别 15,状态 1,行 2
第 2 行: ',' 附近有语法错误。


2、删除表中多余的重复记录(多个字段),只留有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)

此句运行出错,
服务器: 消息 170,级别 15,状态 1,行 1
第 1 行: 'a' 附近有语法错误。
服务器: 消息 156,级别 15,状态 1,行 2
在关键字 'and' 附近有语法错误。

请高手帮忙修改一下错误。
我的目的是想删除表中两个字段的内容完全一样的数据,并且保留重复记录中的最小ID的数据,其余的全部删除

------解决方案--------------------
1.
SQL code
select a.* from vitae a,(select peopleId,seq from vitae group by peopleId,seq  having count(*) >  1)  b
where a.peopleId = b.peopleid and a.seq = b.seq

------解决方案--------------------
delete from vitae a
 where not exists(select peopleId,seq,min(rowid) as rowid 
from vitae 
where peopleId=a.peopleId and seq=a.seq and rowid=a.rowid
group by peopleId,seq having count(*)> 1)