SQL语句求相同班级相同科目分数最高的学生

2025-01-07 04:39:49
推荐回答(4个)
回答1:

用行列转换函数,会不? 可以去找找,百度文库里有这个函数怎么定义。懒得想的可以看下面这个方法:
已知班级,科目的情况,用union all(就是量有点大哈~不过容易理解)
select distinct * from
(
select top 1 * from t where classname='高三(一)班' and course='数学' order by score desc
union all
select top 1 * from t where classname='高三(二)班' and course='数学' order by score desc
…… --(有 X个班级,Y个科目 的话,这就有 X*Y 行啦,懒得动脑就勤点动手吧,哈哈)
)t

回答2:

sql server:
select STUDENTNAME 相同班级相同科目分数最高的学生,CLASSNAME,COURSE,SCORE from(
select RANK() over(partition by CLASSNAME,COURSE order by SCORE desc)rk,STUDENTNAME,CLASSNAME,COURSE,SCORE from t)t1 where rk=1

回答3:

select a.* from t a,
(select max(score) score,classname,course from t group by classname,course) b
where a.classname=b.classname and a.course=b.course and a.score=b.score;

回答4:

select id, name,max(score),class,kemu from chengji
group by class,kemu order by class, kemu