有一个表xxx,如下:
a          b         c         d    
aaa        345       5436      2.30 
aaa        345       5436      2.30
aaa        23        38464     23.00
bbb        138       12        32.60 
bbb        138       12        32.60
bbb        164       9678      13.10
将上表转入yyy,因误操作,导致表中数据重复插入,如下:
a          b         c         d    
aaa        345       5436      2.30 
aaa        345       5436      2.30
aaa        23        38464     23.00
bbb        138       12        32.60 
bbb        138       12        32.60
bbb        164       9678      13.10
aaa        345       5436      2.30 
aaa        345       5436      2.30
aaa        23        38464     23.00
bbb        138       12        32.60 
bbb        138       12        32.60
bbb        164       9678      13.10
现在xxx表已经删除了,现在该怎么删除yyy表中重复的数据?(注意:原xxx表中就有重复数据,这些数据是要保留的)

解决方案 »

  1.   

    select distinct * into ccc from yyy
    select * from ccc
    drop table yyy
      

  2.   

    select distinct * into #tmp from yyy --取出表中所有数据[Distinct]至临时表
    truncate table yyy --删除表内容
    insert yyy select * from #tmp --从临时表中返回表内容
    drop table #tmp --删除临时表
      

  3.   

    --简单方法
    select distinct * into #temp from yyy
    truncate table yyy
    Insert into yyy
    Select * from #temp
    drop table #temp
      

  4.   

    晕,写字本里面复制出来,错位成这样,呵呵,重新编排一下,把下面的放到分析器中执行就行了:
    -----------------------------------------------------------------------------------
    select distinct * into #tmp from yyy --取出表中所有数据[Distinct]至临时表
    truncate table yyy --删除表内容
    insert yyy select * from #tmp --从临时表中返回表内容
    drop table #tmp
      

  5.   

    这样试下:1、
    select IDENTITY(int,1,1) as id,
    *
    into #t
    from yyy2、
    select * from #t order by id
    看看不重复的数据是不是都在前面,如果不是,那好像没有好方法了,如果都在前面,找出开始重复的id,如id1,继续下一步3、
    delete #t where id>=id14、
    truncate table yyy5、
    insert yyy
    select a,b,c,d from #t6、
    drop table #t
      

  6.   

    各位大侠,我原XXX表中就有重复数据的,你们用distinct不是把我原来的一些数据也删除了??
      

  7.   

    研究了一下午,自己解决:
    select IDENTITY(int,1,1) as id,* into #t1 from yyy order by a,b,c,d
    select a,b,c,d,count(*) qty into #t2 from yyy group b a,b,c,ddeclare @a char(4)
    declare @b int
    declare @c int
    declare @d int
    declare aaa cursor for
    select * from #t2
    open aaa
    while 0=0 begin
    fetch next from aaa into @a,@b,@c,@d
    if @@fetch_status<>0 break
    delete from #t1 where a=@a and b=@b and c=@c and d=@d 
    and id<(select min(id) from #t1 where a=@a and b=@b and c=@c and d=@d)+qty/2--重复插入一次为qty/2,二次为qty/3.....以此类推
    end
    close aaa
    deallocate aaaalter table #t1 drop column id
    delete from yyy
    insert into yyy select * from #t1drop table #t1
    drop table #t2嘻嘻,不知道自己可不可以给自己加分