SQL删除重复数据方法
本文转自
http://www.cnblogs.com/sunxiaonan/archive/2009/11/24/1609439.html
文中sql稍作了美化。
例如:
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))
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
select * from peoplewhere peopleId in (select peopleId from people group by peopleId having count(peopleId) > 1)
delete from peoplewhere 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)
select * from vitae awhere (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1)
delete from vitae awhere (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)
select * from vitae awhere (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)
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 integerdeclare cur_rows cursor local for select 主字段,count(*) from 表名 group by 主字段 having count(*) >; 1open cur_rowsfetch cur_rows into @id,@maxwhile @@fetch_status=0beginselect @max = @max -1set rowcount @maxdelete from 表名 where 主字段 = @idfetch cur_rows into @id,@maxendclose cur_rowsset rowcount 0
select distinct * from tableName
select distinct * into #Tmp from tableNamedrop table tableNameselect * into tableName from #Tmpdrop table #Tmp
select identity(int,1,1) as autoID, * into #Tmp from tableNameselect min(autoID) as autoID into #Tmp2 from #Tmp group by Name,autoIDselect * from #Tmp where autoID in(select autoID from #tmp2)
select * from tablename where id in (select id from tablenamegroup by idhaving count(id) > 1)