select *,(select count(*)+1 from 表 where f3=tem.f3 and f2<tem.f2) 排名 from 表 tem

解决方案 »

  1.   

    举例:declare @你的表 table (f1 int,f2 int,f3 varchar(10))
    insert @你的表 values(1,23,'a')
    insert @你的表 values(2,45,'a')
    insert @你的表 values(3,35,'a')
    insert @你的表 values(4,43,'b')
    insert @你的表 values(5,23,'b')
    insert @你的表 values(6,36,'a')select * from (
    select *,(select count(*)+1 from @你的表 where f3=tem.f3 and f2<tem.f2) 排名 from @你的表 tem
    ) aa order by f3,排名
    /*f1          f2          f3         排名          
    ----------- ----------- ---------- ----------- 
    1           23          a          1
    3           35          a          2
    6           36          a          3
    2           45          a          4
    5           23          b          1
    4           43          b          2(所影响的行数为 6 行)*/
      

  2.   

    select * from (
    select *,(select sum(1) from @你的表 where f3=tem.f3 and f2<=tem.f2) 排名 from @你的表 tem
    ) aa order by f3,排名
      

  3.   

    select * from (
    select *,(select count(*)+1 from @你的表 where f3=tem.f3 and f2<tem.f2) 排名 from @你的表 tem
    ) aa order by f3,排名