select (select count(*)+1 from 表 where 成绩>a.成绩) as 排名,*
from 表 a
order by 成绩 desc

解决方案 »

  1.   


    select (select count(*)+1 from 表 where 成绩>a.成绩) as 排名,*
    from 表 a
    order by 成绩 desc或;
    Select *,Identity(int,1,1)As 排名 Into #tmp From 表 Order By 成绩 Desc
    Select * From #tmp Where .....
      

  2.   

    --一般的做法,考虑到成绩相同并列的情况
    select 排名=(select count(distinct 成绩) from 成绩表 where 成绩>=a.成绩),*
    from 成绩表 a
      

  3.   

    --并列的成绩的第二种排序方法:select 排名=(select count(成绩) from 成绩表 where 成绩>a.成绩)+1,*
    from 成绩表 a/*--下面是两种排序方法的不同之处说明,假设有如下数据成绩
    100
    98
    89
    89
    80--按第一种方法,结果是:
    排名 成绩
    1    100
    2    98
    3    89
    3    89
    4    80
    --按第二种方法,结果是:
    排名 成绩
    1    100
    2    98
    3    89
    3    89
    5    80  --第四名已经没有了,因为第三名并列
    --*/
      

  4.   


    --如果是数据很多,用临时表的效率反而会高一点
    select 排名=0,* into #t from 成绩表 order by 成绩 desc--要求是第一种排序结果的处理方法
    declare @成绩 int,@id int
    set @id=0
    update #t set @id=case @成绩 when 成绩 then @id else @id+1 end
    ,排名=@id,@成绩=成绩
    --要求是第一种排序结果的处理方法
    declare @成绩 int,@id int,@id1 int
    select @id=0
    update #t set @id1=case @成绩 when 成绩 then @id1+1 else 1 end
    ,@id=case @成绩 when 成绩 then @id else @id+@id1 end
    ,排名=@id,@成绩=成绩