学生考试, 一年考试N次, 取各学科平均分排名前10的学生  select a.* from 

select studentid, avg(score) as score1, subject from tableA  
group by studentid , subject   
) a 
where  

select count(1) from ( 
select studentid, avg(score) as score1, subject from tableA  
group by studentid , subject )b 
where a.studentid = b.studentid and a.subject  = b.subject  and b.score1>  a.score1)  < 10 

order by a.subject ,  a.score1 desc 

解决方案 »

  1.   

    各学科平均分排名前10的学生   ?应该是各学科得分排名前10的学生吧?select * from tb t
    where score in 
    (
      select top 10 score from tb where subject=t.subject order by score desc
    )
    order by subject , score desc
      

  2.   

    --明白了,是一个人一年当中这个学科的平均分.
    select t.* from 
    (
      select subject , studentid , avg(score) score from tb group by subject , studentid 
    ) tb t
    where score in 
    (
      select top 10 score from (select subject , studentid , avg(score) score from tb group by subject , studentid) m where subject=t.subject order by score desc
    )
    order by subject , score desc
      

  3.   

    select *
    from (select rownum ,subject , studentid , avg(score) score from tb group by subject , studentid order by score) 
    where rownum <= 10