初表如下: 
ID NAME SCORE 
1 A 86 
2 B 77 
3 C 94 
4 D 77 写一段SQL语句,让他变成如下: 
ID NAME SCORE 
1 C 94 
2 A 86 
3 B 77 
3 D 77 注意:不是用ID DESC。他的最后一个ID是3,不是4。 
我想这样做: C的成绩最高,0人比他高,+1=1 
A的成绩第二,1人比他高,+1=2 
B和D的成绩第三,2人比他高,+1=3 
就是小弟初学SQL,不会写出来,请求高人帮解决一下。 
问题补充:请注意不是用SCORE DESC来做,这样ID还是会有4个. 
我的最后一个ID要求是3不是4,谢谢.

解决方案 »

  1.   

    declare @tb table(id int,name varchar(50),score int)
    insert into @tb select 1,'a',86
    insert into @tb select 2,'b',77
    insert into @tb select 3,'c',94
    insert into @tb select 4,'d',77
    insert into @tb select 6,'e',37select rank() over(order by score desc) as orderid,name,score
    from @tb--1    c    94
    --2    a    86
    --3    b    77
    --3    d    77
    --5    e    37select dense_rank() over(order by score desc) as orderid,name,score
    from @tb--1    c    94
    --2    a    86
    --3    b    77
    --3    d    77
    --4    e    372种结果
      

  2.   

    可以给分的,我需要的是SQL 2000的语句.
      

  3.   

    果然是高手,可是我只看的懂SQL 2000的语句,麻烦你写个2000的好吗,谢谢你了高手,分我会照给的.
      

  4.   

    路过写一个,楼主可结贴了。。
    --> --> (Roy_88)生成測試數據
     
    declare @T table([ID] int,[NAME] nvarchar(1),[SCORE] int)
    Insert @T
    select 1,N'A',86 union all
    select 2,N'B',77 union all
    select 3,N'C',94 union all
    select 4,N'D',77Select 
    [名次]=(select count(distinct [SCORE]) from @T where [SCORE]>=a.[SCORE])
    ,[NAME]
    ,[SCORE]
    from @T a
    order by [名次](4 行受影响)
    名次          NAME SCORE
    ----------- ---- -----------
    1           C    94
    2           A    86
    3           B    77
    3           D    77(4 行受影响)
      

  5.   


    create table tb (id int,name varchar(50),score int)
    insert into tb select 1,'a',86
    insert into tb select 2,'b',77
    insert into tb select 3,'c',94
    insert into tb select 4,'d',77
    insert into tb select 6,'e',37
    --借住临时表也可以
    select distinct identity(int,1,1) as id, score 
    into # from tb order by score desc
    select a.id,b.name,b.score from # a inner join tb b on a.score = b.score 
    order by b.score desc