如何删除同一个表中的重复记录,只有3个字段重复。但不是全部删除掉。的留下一条把

谢谢
---------------------------------------------------------------

参照
http://expert.csdn.net/Expert/topic/431/431723.xml?temp=.1738245
---------------------------------------------------------------

select into #tmp distinct col1 from table
delete from table t join #tmp tm on t.col1<>tm.col1
delete from #tmp

select into #tmp distinct col2 from table
delete from table t join #tmp tm on t.col2<>tm.col2
delete from #tmp

select into #tmp distinct col3 from table
delete from table t join #tmp tm on t.col3<>tm.col3

---------------------------------------------------------------

先用distinct把唯一的数据插入临时表,然后删除旧表数据,再把临时表的数据写回去。

---------------------------------------------------------------

selcet min(重复字段1),min(重复字段2),min(重复字段3),别的字段
into new table
from talbe

new table 就是你要的表

---------------------------------------------------------------

select identity(int,1,1),* into #tmp from yourtable

delete from #tmp where x not in (select max(x) from #tmp group by a,b,c )

truncate yourtable

insert yourtable select (除x以外的字段) from #tmp

Published At
Categories with 数据库类
Tagged with
comments powered by Disqus