将表中相同学科,不同班级中的最高分排序列出
SELECT a.className,a.subject,a.maxScore
FROM(SELECT className,test.subject,MAX(score) maxScore
FROM test
GROUP BY test.subject,className) a ORDER BY a.maxScore DESC
userId subject className score
001 subject1 class1 90
002 subject1 class1 91
003 subject1 class1 89
004 subject1 class2 91
005 subject1 class2 92
006 subject1 class2 80
007 subject2 class1 50
008 subject2 class1 51
009 subject2 class1 49
010 subject2 class2 51
011 subject2 class2 52
012 subject2 class2 40
执行后:
className subject maxScore
class2 subject1 92
class1 subject1 91
class2 subject2 52
class1 subject2 51