表结构:
 
cid         type
1            sh
2            sh
3            gj
4            gj
5            bj
6            bj
--------------------------------
需要的效果
cid        type
1          sh
1          sh
2          gj
2          gj
3          bj
3          bj

解决方案 »

  1.   


    update a
    set a.cid = isnull((select min(cid) from tb where [type] = a.[type]),a.cid)
    from tb a
      

  2.   


    declare @t table (cid int,type varchar(2))
    insert into @t
    select 1,'sh' union all
    select 2,'sh' union all
    select 3,'gj' union all
    select 4,'gj' union all
    select 5,'bj' union all
    select 6,'bj';with maco as
    (
    select row_number() over (order by min(cid)) as rid,type from @t group by type
    )
    select b.* from @t a left join maco b on a.type=b.type
    /*
    rid                  type
    -------------------- ----
    1                    sh
    1                    sh
    2                    gj
    2                    gj
    3                    bj
    3                    bj
    */
      

  3.   

    ----------------------------------------------------------------
    -- Author  :fredrickhu(小F,向高手学习)
    -- Date    :2011-08-05 13:02:08
    -- Verstion:
    --      Microsoft SQL Server 2008 (RTM) - 10.0.1600.22 (Intel X86) 
    -- Jul  9 2008 14:43:34 
    -- Copyright (c) 1988-2008 Microsoft Corporation
    -- Enterprise Evaluation Edition on Windows NT 5.1 <X86> (Build 2600: Service Pack 3)
    --
    ----------------------------------------------------------------
    --> 测试数据:[tb]
    if object_id('[tb]') is not null drop table [tb]
    go 
    create table [tb]([cid] int,[type] varchar(2))
    insert [tb]
    select 1,'sh' union all
    select 2,'sh' union all
    select 3,'gj' union all
    select 4,'gj' union all
    select 5,'bj' union all
    select 6,'bj'
    --------------开始查询--------------------------
    select
      (select  COUNT(distinct type) from tb where cid<=t.cid) as cid,TYPE
    from
      tb t
    ----------------结果----------------------------
    /* cid         TYPE
    ----------- ----
    1           sh
    1           sh
    2           gj
    2           gj
    3           bj
    3           bj(6 行受影响)
    */
      

  4.   

    谢谢 各位的答案,目前各位的答案都有局限,因为类型是不固定的,我的想法就是想让同一类型拥有一个cid