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

请教快速删除重复数据
请教快速删除重复数据
我有一个表   数据很                                      
多   大概几十万条   有部分重复

现在需要按照   其中2个字段的不重复f_name,f_add来保留唯一数据


f_id,f_name,f_add,f_work,f_age
1,a,b,c,d
2,a,b,d,e
3,a,c,d,e

那么需要得到
1,a,b,c,d
3,a,c,d,e
请教如何实现
一条SQL语句
不要用临时表


------解决方案--------------------
select * from f
where not exist
(select 1 from f f1 where f1.f_id <f.f_id and f1.f_name=f.f_name and f1.f_add=f.f_add)
------解决方案--------------------
delete t
where exists(select 1 from t a where t.f_add=a.f_add and t.f_name=a.f_name and t.id> a.id )
------解决方案--------------------
declare @t table(f_id int,f_name varchar(10),f_add varchar(10),f_work varchar(10),f_age varchar(10))
insert @t
select 1, 'a ', 'b ', 'c ', 'd ' union all
select 2, 'a ', 'b ', 'd ', 'e ' union all
select 3, 'a ', 'b ', 'd ', 'e '

----查询同名中f_id最大的行
select * from @t as a where not exists(select 1 from @t where
f_name = a.f_name and f_add = a.f_add and f_work = a.f_work
and f_age = a.f_age and f_id > a.f_id)

----删除同名行,只保留f_id最大的行
delete a from @t as a where exists(select 1 from @t where
f_name = a.f_name and f_add = a.f_add and f_work = a.f_work
and f_age = a.f_age and f_id > a.f_id)

----查看删除后的效果
select * from @t


/*结果
1,a,b,c,d
3,a,c,d,e
*/