我尝试了很多但没有成功,我想只显示学生名单中学生分数的最大平均值,如下表所示。
我的表
我想得到如下结果
预期输出
到目前为止我做了什么
SELECT MAX(a.Total_Qty) As TotalMax,a.studentId
FROM(
SELECT AVG( s.marks ) AS Total_Qty,s.studentId
FROM results s
WHERE s.stream = 'Form One'
GROUP BY s.studentId) AS a
答案 0 :(得分:2)
内部查询将为您提供每个学生的平均值列表。 然后我们按其平均分数(降序)排序,最后得到前1名(限制1)
SELECT a.studentId, a.Total_Qty as MaxAvg
FROM(
SELECT AVG( s.marks ) AS Total_Qty,s.studentId
FROM results s
WHERE s.stream = 'Form One'
GROUP BY s.studentId)
AS a
Order by a.Total_Qty Desc
Limit 1
可替换地:
SELECT AVG( s.marks ) AS Total_Qty,s.studentId
FROM results s
WHERE s.stream = 'Form One'
GROUP BY s.studentId
Order By AVG( s.marks ) Desc
Limit 1
答案 1 :(得分:-1)
(UNTESTED)我希望有所帮助
如果您使用的是MSSQL:
SELECT TOP(1) studentId, AVG(marks) FROM results GROUP BY studentId
ORDER BY MAX(AVG(marks)) Desc
如果您使用的是SQL:
SELECT studentId, AVG(marks) FROM results GROUP BY studentId
ORDER BY MAX(AVG(marks)) Desc Limit 0,1