SQL2000 我有一个成绩表,列如下
标示ID(id),用户名(uid),成绩(score),考试用时(exemtime),科目(exemid)
要取到每个人的最高成绩,成绩相同的就取时间最短的那个,时间分数都相同就按ID取,这个是每门每个人可以考两次
有部分数据如下  id      uid      score      exemtime      exemid  
   1      张三      100         1200         语文
   2      张三       90          800         数学
   3      张三      100         1100         语文
   4      李四       89         1300         语文
   5      李四       90         1100         语文
   6      李四       90         1300         数学
   7      王五       90         1300         数学
   8      王五       98         1300         数学
   9      王五       98         1300         语文
要得到的结果是  id      uid      score      exemtime      exemid  
   3      张三      100         1100         语文
   5      李四       90         1100         语文
   8      王五       98         1300         数学 求SQL语句,或建视图根据视图查,最好不要是存储过程

解决方案 »

  1.   

    select * from tb a
     where score=(select max(score) from tb where uid=a.uid)
      

  2.   

    with cte as
    (
      select row_number() over(partition by uid order by score desc,exemtime,id) no,* 
          from tb
    )select * from cte where no=1
      

  3.   


    select *
    from (
        select *,rid = row_number() over(partition by uid order by score desc,exemtime,id)
        from tb
    )t
    where rid = 1
      

  4.   

    select * from tb a
     where score=(select max(score) from tb where uid=a.uid)
    and exemtime =(selct min(exemtime) from tb where uid= a.uid)
      

  5.   

    select * from tb a
     where score=(select max(score) from tb where uid=a.uid)
    and exemtime =(selct min(exemtime) from tb where uid= a.uid)
      

  6.   


    select *
    from tb t
    where id = (select top 1 id from tb where uid = t.uid order by score desc,exemtime,id)
      

  7.   

    create table tb(id int ,uid varchar(8),score int,exemtime int,exemid varchar(8))insert tb
    select 1,'张三',100,1200,'语文' union all
    select 2,'张三',90,800,'数学' union all
    select 3,'张三',100,1100,'语文' union all
    select 4,'李四',89,1300,'语文' union all
    select 5,'李四',90,1100,'语文' union all
    select 6,'李四',90,1300,'数学' union all
    select 7,'王五',90,1300,'数学' union all
    select 8,'王五',98,1300,'数学' union all
    select 9,'王五',98,1300,'语文'select * from tb a
    where id in
    (select top 1 id from tb b where a.uid=b.uid order by score desc,exemtime,id)/*
    id          uid      score       exemtime    exemid   
    ----------- -------- ----------- ----------- -------- 
    3           张三       100         1100        语文
    5           李四       90          1100        语文
    8           王五       98          1300        数学(所影响的行数为 3 行)
    */
      

  8.   

    select *
    from table as t
    where id = (select top 1 id from table as tb where tb.uid = t.uid order by tb.score desc)