代码&优雅着&生活

导航

学校有多个班级,有语数外多门成绩,按照成绩排序后,找出每个班级语文成绩前五名学生

我只是记录一下




select *
from (select a.*,b.score,
dense_rank() over(partition by b.classsid order by b.score desc) rn
from student a,score b
where a.studentId=b.id and b.classsid=a.classsid)
where rn<=5


select *
from (select a.*,b.score,
rank() over(partition by b.classsid order by b.score desc) rn
from student a,score b
where a.studentId=b.id and b.classsid=a.classsid)
where rn<=5


已经验证可以的方案

select * from (

select t.id , t.sname , t.china , t.math , t.english ,
t.glass,t.china + t.math + t.english as total,dense_rank()
over(partition by t.glass order by t.china + t.math + t.english desc) a from t_score t

)where a<=5 order by total desc

 

还有一种比较熟悉的方案

Select a.*,(a.china + a.math + a.english) sums
    From t_score a
   Where (Select Count(*)
            From t_score b
           Where a.glass = b.glass
             And (a.china + a.math + a.english) <
                 (b.china + b.math + b.english)) < 3
   order by  a.glass,sums desc

 

posted on 2016-02-19 17:37  幸运的凌人  阅读(548)  评论(0编辑  收藏  举报