现有一表,只有两个字段
     姓名        分数
     aa          100
     bb          99
     cc          100
     dd           96    现在要用一条sql语句按名次进行排序,达到下面的效果,请问如何写呢 名次    姓名        分数
  1       aa          100
  1       cc          100
  3       bb          99
  4       dd          96

解决方案 »

  1.   

    create table test
    (姓名 varchar(10),
     分数 int
    )
    insert test
     select     'aa'  ,        100 union all
     select       'bb' ,         99 union all
      select      'cc' ,         100 union all
       select     'dd'  ,         96
    select * from test
    select 名次=(select count(*)+1 from test where 分数>a.分数),姓名,分数 from test a
    order by  名次
     
    drop table test
      

  2.   

    姓名         分数          
    ---------- ----------- 
    aa         100
    bb         99
    cc         100
    dd         96(所影响的行数为 4 行)名次          姓名         分数          
    ----------- ---------- ----------- 
    1           aa         100
    1           cc         100
    3           bb         99
    4           dd         96(所影响的行数为 4 行)
      

  3.   

    declare @t table(姓名 varchar(10),分数 int)
    insert into @t select     'aa'          ,100
    union all select  'bb'          ,99
    union all select   'cc'          ,100
    union all select  'dd'           ,96select 名次=(select count(*)+1 from @t where 分数>a.分数),* from @t a order by 名次