MYSQL分组列和等级

时间:2019-08-12 08:41:23

标签: php mysql

I'd.  Regno.  Class.    Subject      Score   
1.     111.      JSS1.    Mth.            60
2.     112.       JSS1.    Mth.           50
3.      111        JSS1.    Eng.          60
4.      112.       JSS1.    Eng.          80 etc

请问,如何在不指定主题的情况下如何使用Php和MySQL使用上述记录来动态获取以下记录?

Regno.      Eng.        Math.    Total.    Position
111.        60.         60.         120.       2nd
112.        80.         50.          130.      1st

我在这个论坛上向所有程序员致意,几天前我在此发布了帖子,并给出了以下答案,谢谢。但是我不想指定主题(例如Mth,Eng等),我想要一个可以动态实现的高级示例代码?谢谢。

select t.*
, rank() over (order by total) as ranking
from    (
select regno
, sum(case when subject = 'Mth.' then score end) as math
, sum(case when subject = 'Eng.' then score end) as eng
, sum(score) as total
from test.scores
group by regno
) t

0 个答案:

没有答案