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

删除重复的数据

A     B
1     1
1     2
2     1
3     1
4     1
4     2
5     1

变成
A   B
1   1
2   1
3   1
4   1  
5   1

多余的数据删除,怎么实现,呵呵?

------解决方案--------------------
举个例子给楼主参照一下:
create table ta(id int, name varchar(2), hit int)
insert ta select 1, 'zx ', 11
insert ta select 1, 'zx ', 54
insert ta select 1, 'zx ', 15
insert ta select 2, 'jl ', 45
insert ta select 2, 'jl ', 84
insert ta select 2, 'jl ', 87

alter table ta add con int identity(1,1) not null--新增列


delete a
from ta a
where exists(select 1 from ta where id=a.id and name=a.name and con <a.con)

alter table ta drop column con--删除新增列

select * from ta


id name hit
----------- ---- -----------
1 zx 11
2 jl 45

(2 行受影响)

------解决方案--------------------
blueonly(认真编程,低调生活。) ( ) 信誉:100 Blog 2007-04-02 11:01:28 得分: 0


保证b字段是个唯一的字段,上面的方法都可以了吧?谢谢了,就这样了,解决了


-----------
如果B是唯一的,或者在A相同的時候,B不會重復這兩個方法都可以用。


--方法一:(上面錯誤了一點)
Delete A From 表 A
Where B Not In (Select Min(B) From 表 Where A = A.A)
--方法二
Delete A From 表 A Where Exists(Select A From 表 Where A = A.A And B < A.B)