又一个表,如下:
字段: KK   a1  a2  a3 .....
内容: 1    3    5   8 .....
      2    4    8   8 .....
      1    5    5   4 .....
      3    2    1   5 .....
     ...   ...  ...  ....要想只保留KK段值唯一,重复的就不要,比如第3条记录就不要了,有简单快速的办法么?

解决方案 »

  1.   

    Declare @KK int,@Cnt intDeclare curCnt Cursor For 
    Select KK,Count(1) Cnt From Table Group By KK Having Count(KK) > 1Fetch Next From curCnt Into @KK,@Cnt
    While @@Fetch_Status <> 0
    BeginSet @Cnt = @Cnt - 1
    Set @@RowCount = @Cnt
    Delete Table Where KK = @KK
    Set @@RowCount = 0Fetch Next From curCnt Into @KK,@Cnt
    End
    大概就是这个样子,可能语法会有不对的地方,我没有试过!
    思路就是这样的 ̄ ̄ ̄
      

  2.   

    如果有主键或者唯一键来唯一确定一条记录, 则可以用下面的语句实现:select * from tb a
    where not exists(
        select * from tb where id<a.id and kk=a.kk)
      

  3.   

    先在kk字段建立索引:
    create index table1 on table1(kk)
    再申明游标:
    declare @c1 varchar(100),
    @c2 varchar(100)
    declare atmp1 cursor for 
    select kk from table1
    for update
    open atmp1
    fetch next from atmp1 into @c1
    while @@fetch_status=0
    begin
    fetch next from atmp1 into @c2
    if @c1=@c2
    begin
    delete from table1 where current of atmp1
    end
    else
    begin
    set @c1=@c2
    end
    end
    close atmp1
    deallocate atmp1
    可以实现你的目的。
      

  4.   

    select distinct kk from table
      

  5.   

    zfl2k(风)的方法只能得到一列的,呵呵.lzhs(快乐至上)和Eway5(Eway) 都是存储过程了。
    表没有唯一键,zjcxc(邹建)的也不行了。
    想用视图,最好不要用存储过程、函数什么的,能行么?
    其实邹老大说不行,我就觉得够呛能实现了,不过还想放两天再结帖,呵呵。
      

  6.   

    alter table tb  add [xh] [int] IDENTITY (1, 1) NOT NULL 
    godelete from tb
    where (xh not in
              (select min(xh) as tt
             from tb group by kk))
    goalter table tb drop column [xh]
    go
      

  7.   

    Select t.*
    From Table t,
    (Select KK,Min(A1) A1 From Table Group By KK) T2  --这里根据KK分组,取出一个最小的
                                               --A1,也可以是其他列,然后再与原表连接查询
    Where t.KK = T2.KK And t.A1 = T2.A1
      

  8.   

    delete from A
      group by num 
      having count(Name) >1
      这样的话就把所有重复的都删除了。