a     b     c
    1     1     1
    1     2     2
    1     3     3
    2     1     4
    2     2     5
    3     1     6
    3     2     7
    3     3     8
要得到下面这样的结果
我该怎么去改
    a     b     c
    1     1     1
    1     2     2
    1     3     3
    2     1     1
    2     2     2
    3     1     1
    3     2     2
    3     3     3

解决方案 »

  1.   

    declare @a int;
    declare @c intupdate tb set
        @c = case when @a = a then @c + 1 else 1 end,
        @a = a,
        c = @c;
      

  2.   

    select a,b,c=(select count(1) from tb where a=a.a and b<=a.b) as c
    from tb a
    order by a,b
      

  3.   


    -->Title:生成測試數據
    -->Author:wufeng4552【水族杰纶】
    -->Date :2009-08-26 13:23:57
     
    if not object_id('tb') is null
    drop table tb
    Go
    Create table tb([a] int,[b] int,[c] int)
    Insert tb
    select 1,1,1 union all
    select 1,2,2 union all
    select 1,3,3 union all
    select 2,1,4 union all
    select 2,2,5 union all
    select 3,1,6 union all
    select 3,2,7 union all
    select 3,3,8
    Go
    update tb set c=b
    select * from tb
    /*
    a           b           c
    ----------- ----------- -----------
    1           1           1
    1           2           2
    1           3           3
    2           1           1
    2           2           2
    3           1           1
    3           2           2
    3           3           3(8 個資料列受到影響)
    */
      

  4.   

    ----------------------------------------------------------------
    -- Author :fredrickhu(小F 向高手学习)
    -- Date   :2009-08-26 13:23:17
    ----------------------------------------------------------------
    --> 测试数据:[tb]
    if object_id('[tb]') is not null drop table [tb]
    go
    create table [tb]([a] int,[b] int,[c] int)
    insert [tb]
    select 1,1,1 union all
    select 1,2,2 union all
    select 1,3,3 union all
    select 2,1,4 union all
    select 2,2,5 union all
    select 3,1,6 union all
    select 3,2,7 union all
    select 3,3,8
    --------------开始查询--------------------------select 
      a,b,
      c=(select count(b)+1 from tb where a=t.a and t.b>b)
    from 
      [tb] t
    order by 
      a,b
    ----------------结果----------------------------
    /*a           b           c
    ----------- ----------- -----------
    1           1           1
    1           2           2
    1           3           3
    2           1           1
    2           2           2
    3           1           1
    3           2           2
    3           3           3(8 行受影响)*/
      

  5.   

    我在补充下,都是新纪录,忘记这个了
         a    b    c 
        1    3    1 
        1    4    2 
        1    5    3 
        2    2    4 
        2    3    5 
        3    4    6 
        3    5    7 
        3    6    8 
        a    b    c 
        1    3    1 
        1    4    2 
        1    5    3 
        2    2    1 
        2    3    2 
        3    4    1 
        3    5    2 
        3    6    3 
      

  6.   

    -->Title:生成測試數據
    -->Author:wufeng4552【水族杰纶】
    -->Date :2009-08-26 13:23:57
     
    if not object_id('tb') is null
    drop table tb
    Go
    Create table tb([a] int,[b] int,[c] int)
    Insert tb
    select 1,1,1 union all
    select 1,2,2 union all
    select 1,3,3 union all
    select 2,1,4 union all
    select 2,2,5 union all
    select 3,1,6 union all
    select 3,2,7 union all
    select 3,3,8
    Go
    select [a],
           [b],
           [c]=row_number()over (partition by a order by getdate())
    from tb
    /*(8 個資料列受到影響)
    a           b           c
    ----------- ----------- --------------------
    1           1           1
    1           2           2
    1           3           3
    2           1           1
    2           2           2
    3           1           1
    3           2           2
    3           3           3(8 個資料列受到影響)*/
      

  7.   

    引用 7 楼 lgq_liang 的回复:
    我在补充下,都是新纪录,忘记这个了 
        a    b    c 
        1    3    1 
        1    4    2 
        1    5    3 
        2    2    4 
        2    3    5 
        3    4    6 
        3    5    7 
        3    6    8 
    要的结果是这样的
        a    b    c 
        1    3    1 
        1    4    2 
        1    5    3 
        2    2    1 
        2    3    2 
        3    4    1 
        3    5    2 
        3    6    3 
      

  8.   

    if not object_id('tb') is null
    drop table tb
    Go
    Create table tb([a] int,[b] int,[c] int)
    Insert tb
    select 1,3,1 union all
    select 1,4,2 union all
    select 1,5,3 union all
    select 2,2,4 union all
    select 2,3,5 union all
    select 3,4,6 union all
    select 3,5,7 union all
    select 3,6,8
    Go
    declare @a int;
    declare @c int
    update tb set
        @c = case when @a = a then @c + 1 else 1 end,
        @a = a,
        c = @c
    select * from tb
    /*
    a           b           c
    ----------- ----------- -----------
    1           3           1
    1           4           2
    1           5           3
    2           2           1
    2           3           2
    3           4           1
    3           5           2
    3           6           3(8 個資料列受到影響)
    */