有这样一张表
userID  Num
1        12
2        10
3        14
4        18
我如何获取用户2的排名

解决方案 »

  1.   

    SELECT USERID,NUM,(SELECT COUNT(1) FROM TB WHERE NUM<=T.NUM) RN FROM TB TSELECT USERID,NUM,(SELECT COUNT(1) FROM TB WHERE NUM>=T.NUM) RN FROM TB T
      

  2.   

    select 排名=(select count(1) from tb where num>t.num)
    from tb t
    where userid=2
      

  3.   

    也许要从小到大的select 排名=(select count(1) from tb where num<t.num)+1
    from tb t
    where userid=2刚才忘了+1
      

  4.   

    declare @tb table([userID] int,[Num] int)
    insert @tb
    select 1,12 union all
    select 2,10 union all
    select 3,14 union all
    select 4,18
     select * ,pm =(select COUNT(1)+1 from @tb where Num <a.Num)  from @tb as a order by Num