表A如下
a,   b
12,  34
12,  56
12,  23
13,  33
14,  11
14,  15
如保用一条语句得到
a,   b    c
12,  23   1
12,  34   2
12,  56   3
13,  33   1
14,  11   1
14,  15   2
谢谢

解决方案 »

  1.   

    declare @t table(a int,b int)
    insert into @t select 12,  34
    union all select 12,  56
    union all select 12,  23
    union all select 13,  33
    union all select 14,  11
    union all select 14,  15select *,c=(select count(*)+1 from @t where a=a.a and b<a.b) from @t a order by a,b
      

  2.   

    set nocount on
    declare @a table (a int, b int)
    insert into @a
    select 12,34 union all
    select 12,56 union all
    select 12,23 union all
    select 13,33 union all
    select 14,11 union all
    select 14,15select * from @aselect
    a,
    b,
    (select count(*)+1 from @a where a=a.a and b<a.b) c
    from @a a
    order by a,bset nocount off/*
    a           b           
    ----------- ----------- 
    12          34
    12          56
    12          23
    13          33
    14          11
    14          15a           b           c           
    ----------- ----------- ----------- 
    12          23          1
    12          34          2
    12          56          3
    13          33          1
    14          11          1
    14          15          2
    */
      

  3.   

    ^^;刚去喝了口水,发了一个一样的查询
    (select count(*)+1 from @a where a=a.a and b<a.b) c
    改成
    (select count(*) from @a where a=a.a and b<=a.b) c
    也一样
      

  4.   

    在查询分析器里一执行 有下列错
    服务器: 消息 156,级别 15,状态 1,行 1
    Incorrect syntax near the keyword 'table'.
    服务器: 消息 170,级别 15,状态 1,行 10
    Line 10: Incorrect syntax near '@a'.
      

  5.   

    哦,忘了说了,我用的是SQL 7.0好象7.0里不支持表量,语句应该没错
      

  6.   

    这样啊。。换成表就可以了create table tb(a int,b int)
    insert into tb select 12,  34
    union all select 12,  56
    union all select 12,  23
    union all select 13,  33
    union all select 14,  11
    union all select 14,  15select *,c=(select count(*)+1 from tb where a=a.a and b<a.b) from tb a order by a,bdrop table tb