获得 A 数最多的学生

Get student that has the highest number of A's

例如,我们有 table Student_Marks,其中包含列 Mark_ID(例如 1、2、3、4、5)和 Mark_Name(E, D,C,B,A),我们有一个 table 学生与 Student_Marks 有关系。我们如何才能获得 A 数最多的学生。谢谢!

由于您没有提供确切的 table 结构和任何示例数据,所以这个答案将基于我从您的问题中可以理解的内容。

假设 Student table 是这样的:

Student_ID  Student_Name    Marks_ID
    1         John Doe          1
    1         John Doe          1
    1         John Doe          2
    1         John Doe          3
    1         John Doe          4
    1         John Doe          5
    1         Jane Doe          1
    1         Jane Doe          2
    1         Jane Doe          2
    1         Jane Doe          2
    1         Jane Doe          4

Student_Marks table 是这样的:

Marks_ID    Mark_Name
   1            A
   2            B
   3            C
   4            D
   5            E

然后你可以使用下面的查询:

SELECT      TOP 1 students.Student_Name, 
            COUNT(marks.Mark_Name) AS TotalOfGradeA
FROM        Student students
INNER JOIN  Student_Marks marks ON students.Marks_ID = marks.Marks_ID
WHERE       marks.Mark_Name = 'A'
GROUP BY    students.Student_Name
ORDER BY    COUNT(marks.Mark_Name) DESC

注意:您尚未指定要如何处理 'ties',因此我也编写了以任何特定方式处理它的查询。

你可以在这里看到这个 -> http://rextester.com/SFVLA27100

希望对您有所帮助!!!