表中某个指标重复,去掉重复项:
select *
from #temp
where A0107
in (select A0107 from #temp
group by A0107
having COUNT(A0107)>1
) and id not in (
select MIN(id) from #temp
group by A0107
having COUNT(A0107)>1
)
select * from #temp
--实现sql server 中类似于oracle中rownum的关键字,
--我想取出符合条件的第301-310条记录,又不想取出所有数据,有没有好的解决方案?
--插入数据1,2,3,4
select identity(int,1,1) as id,* into #temp from UsrA01
select * from #temp where id between 301 and 310
--1,10插入的数据1,11,21,31,跳十个
select identity(int,1,10) as id,* into #temp1 from UsrA01
select * from #temp1 where id between 301 and 310
多个字段分组
删除表中多余的重复记录(多个字段),只留有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.
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)
在应使用条件的上下文(在 ',' 附近)中指定了非布尔类型的表达式
多条件时候不能用in 要用exists