显示重复的最大值

时间:2017-07-30 04:50:26

标签: mysql sql

我有一张桌子:

s

我想找到由Assessment_Type分组的最大标记,如果有重复,则显示两者,所以在这种情况下:

+----------------+----------------+------------------+-------+
|   Student_Id   |  Student_Name  |  Assessment_Type | Mark  |
+----------------+----------------+------------------+-------+
|   300          |      John      |   Assignment     |  1    |
|   200          |      Liz       |    Final         |  2    |
|   300          |      John      |   Mid-term       |  3    |
|   100          |      Sue       |   Mid-term       |  4    |
|   200          |      Liz       |    Project       |  5    |
|   300          |      John      |   Assignment     |  6    |
|   200          |      Liz       |    Final         |  7    |
|   300          |      John      |   Mid-term       |  8    |
|   100          |      Sue       |   Mid-term       |  8    |
|   200          |      Liz       |    Project       |  9    |
+----------------+----------------+------------------+-------+

我只展示了其中一个:

+----------------+----------------+------------------+-------+
|   Student_Id   |  Student_Name  |  Assessment_Type | Mark  |
+----------------+----------------+------------------+-------+
|   300          |      John      |   Assignment     |  6    |
|   200          |      Liz       |    Final         |  7    |
|   300          |      John      |   Mid-term       |  8    |
|   100          |      Sue       |   Mid-term       |  8    |
|   200          |      Liz       |    Project       |  9    |
+----------------+----------------+------------------+-------+

编辑:添加了更多详细信息。

2 个答案:

答案 0 :(得分:1)

您可以在每种评估类型中找到最大标记,然后将最大值(标记)和评估类型与每行进行比较。

select * from STUDENTS_ASSESSMENT where (Assessment_Type,marks) in 
((select Assessment_Type,max(marks) from STUDENTS_ASSESSMENT groupby Assessment_Type));

答案 1 :(得分:0)

您可以找到Mark列的最大值,然后选择Mark值等于此值的行:

SELECT 
  Student_Id, Student_Name, Assessment_Type, Mark
FROM
  STUDENT_ASSESSMENT FullSet
JOIN
  (SELECT MAX(Mark) AS Mark FROM STUDENT_ASSESSMENT) MaxMark
USING (Mark);