以下语句是得出一个排名的字段,但是只有CNT值相同则下一个名次就不对了,如截图,请问如何解决?
select *,(select count(1)+1 from  loginCnt a where a.cnt>b.cnt) as 名次
from loginCnt b order by cnt desc

解决方案 »

  1.   

    loginName cnt  名次
    fish 38 1
    9999 21 2
    cars 20 3
    hyjia 19 4
    58148 16 5
    50335 16 5
    yong1 7 7
    2246 6 8
    58148 6 8
    82675 6 8
    9999 4 11
      

  2.   

    select * ,rn=dense_rank()over(order by cnt desc)
    from loginCnt
      

  3.   

    SQL2000这样用-- count(DISTINCT a.cnt)
    select *,(select count(DISTINCT a.cnt)+1 from loginCnt a where a.cnt>b.cnt) as 名次
    from loginCnt b order by cnt desc
      

  4.   

    select *,
    (select count(1)+1 from loginCnt a where a.cnt>b.cnt) as 名次
    from loginCnt b order by 名次 desc