表:學號  考試第次    時間      成績
1      第一次     2004..     55
1      第二次     2002..     80
1      第三次     2001..     90
2      第一次     2002..     20
2      第二次     2004..     80
3      ....       ...        ..
3
4      ..          ..         ..
4      ..          ..        ..
...找出每個人成績最高的那一次記錄,如果那個人多次考試中最高成績有相等,找出時間最近的那一次??
用SQL查詢得資料集!!!

解决方案 »

  1.   

    SELECT DISTINCT MAX(成績)FROM Table
      

  2.   

    select Max(成绩) from table where 时间 in (select distinct(时间) from Table)
      

  3.   

    select 学号,考试第次,时间,MAX(成绩) from table where 时间 in (select distinct(时间) from Table) GROUP BY 学号
      

  4.   

    select 学号,考试第次,成绩,max(时间) 时间 
    from 
    (
    select 学号,考试第次,时间,MAX(成绩) as 成绩 from table 
    group by 学号,考试第次,时间) a
    group by 学号,考试第次,成绩上面的代碼一定可以嗎。
      

  5.   

    最好加个字增id效率不高期待更好的create table t(学号 varchar(20),考试第次 varchar(20),时间 datetime,成绩 int)
    insert into t select '1',      '第一次',     '2004-5-1',     55
    union select '1',      '第三次',     '2002-5-2',     80
    union select '1',      '第二次',     '2002-5-3',     80
    union select '2',      '第一次',     '2002-5-1',     20
    union select '2',      '第二次',     '2004-5-2',     80
    select 学号,考试第次,时间,成绩
    from t
    where exists(select 1 from (
                 select 学号,max(时间) as 时间,max(成绩) as 成绩
                 from t
                 where exists(select 1 from 
                             (select 学号,max(成绩) as 成绩 from  t  group by 学号 ) B
                              where t.学号=b.学号 and t.成绩=b.成绩)
                  group by 学号) C
                  where t.学号=c.学号 and t.时间=c.时间 and t.成绩=c.成绩)
    order by 学号