select top 2 学号,max(考试时间) from tablename group by 学号 order by 考试时间 desc

解决方案 »

  1.   

    select * from tb where 学号 in (select top 2 学号 from tb group by 学号 order by count(学号))
    这样写对吗!?
      

  2.   

    select * from tb where 学号 in (select top 2 学号 from tb group by 学号 order by count(学号) desc)
      

  3.   

    回复人: xielongfei(龙飞) ( ) 信誉:99  2005-10-18 22:04:00  得分: 0  
     
     
       select * from tb where 学号 in (select top 2 学号 from tb group by 学号 order by count(学号))
    这样写对吗!? -->再修改一下,
    select * from tb where 学号 in (select top 2 学号 from tb group by 学号 order by count(学号) desc )-->要降序排列,这样才能取到考试次数最多的前两名同学
     iwl() 查询的是考试时间排在最前面的学号,时间,不是楼主要的
      

  4.   

    哦,谢谢!
    CSDN,的确快!!
    结帖
      

  5.   

    create table A
    (   学号  varchar(10),
       考试时间  datetime)
    insert A
    select '0001','2005-02-26' union
    select '0002','2005-05-27' union
    select '0001','2005-05-26' union
    select '0003','2005-04-26' union
    select '0001','2005-03-26' union
    select '0002','2005-02-28' union
    select '0001','2005-07-26' union
    select '0002','2005-02-26' union
    select '0004','2005-08-26' select 学号,max(考试时间)  from A where a.学号 in (select top 2 学号 from A group by 学号 order by count(*) desc) 
    group by  学号