2014-09-05 96 views
0

我已經嘗試了很多沒有成功,我只想顯示下表中顯示的學生列表中學生分數的最大平均值。尋找學生的最高平均分

我的表
enter image description here

我想要得到的結果如下

預期輸出 enter image description here

是我迄今所做

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

您想要學生或所有學生的平均音符嗎? – Youness 2014-09-05 08:38:27

+0

我想讓一個學生獲得最高的平均分 – dxcoder1 2014-09-05 08:47:28

回答

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 
+0

看來你可以刪除查詢包含。只需按AVG(s.marks) – Bulat 2014-09-05 09:05:43

+0

排序感謝它幫助 – dxcoder1 2014-09-05 09:21:07

-1

(未經測試)我希望它能幫助

如果您使用的是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