id        数学         英语          语文
1          90           88            30
         90           70            52
3          50           88            60
4          90           88            40请高手教下我怎么写这个sql语句    我现在要做一个连续的判读    :   我先取数学成绩最好的(如果数学成绩最好的没有重复我就停止取值,直接取出id),如果数学最好的有重复我再取英语成绩最好的(如果英语成绩最好的没有重复我就停止取值取出id)  如果再有重复  我最后比较语文成绩最好的  取出他的id       
求高手赐教啊  ……

解决方案 »

  1.   

    select top 1 from tb order by 数学 desc,英语 desc,语文 desc
      

  2.   

    select top 1 * from tb order by 数学 desc,英语 desc,语文 desc
      

  3.   


    if ...
    ...
    else if ...
    ...
    else
    ...
      

  4.   


    对数学成绩降序  获取到数学的最好成绩
    对英语成绩降序 获取到英语的最好成绩
    对语文成绩降序 获取到语文的最好成绩
    select top 1 * from scoretb order by 数学 desc,英语 desc,语文 desc
      

  5.   

    对不起 是我没有表达清楚    我做这个比较  有个前提,在得到数学成绩Max(并且只有一个)之后  就不对英语和语文做比较了。如果数学Max不止一个,我再对英语取Max(如果也只有一个)后面的语文就不用比较了。   不知道这样说清晰了吗 
      

  6.   

    我也知道这个可能是个 if  else  但是 我写出来的 都不对,所以求助各位大虾   谢谢了
      

  7.   

    你把你的if else贴出来,直接拿你的改,懒得自己写。
      

  8.   

    use Tempdb
    go
    --> --> 
     
    if not object_id(N'Tempdb..#T') is null
    drop table #T
    Go
    Create table #T([数学] int,[英语] int,[语文] int)
    Insert #T
    select 90,88,30 union all
    select 90,70,52 union all
    select 50,88,60 union all
    select 90,88,40
    Go
    Select ID=DENSE_RANK()OVER(ORDER BY [数学] desc,[英语]desc,[语文]desc),* 
    from #T
    /*
    ID 数学 英语 语文
    1 90 88 40
    2 90 88 30
    3 90 70 52
    4 50 88 60
    */
      

  9.   


    create table sc(id int, 数学 int, 英语 int, 语文 int)insert into sc
    select 1, 90, 88, 30 union all
    select 2, 90, 70, 52 union all
    select 3, 50, 88, 60 union all
    select 4, 90, 88, 40
    declare @mm int,@me int,@mc intselect @mm=max(数学),@me=max(英语),@mc=max(语文) from scif (select count(*) from sc where 数学=@mm)>=2
    begin
      if (select count(*) from sc where 英语=@me)>=2
      begin
         if (select count(*) from sc where 语文=@mc)>=2
         begin
           print null
         end
         else
         begin
           select * from sc where 语文=@mc
         end
      end 
      else
      begin
         select * from sc where 英语=@me
      end   
    end
    else
    begin
      select * from sc where 数学=@mm
    endid          数学          英语          语文
    ----------- ----------- ----------- -----------
    3           50          88          60