2016-07-13 110 views
0

我目前使用下面的查詢,以獲得最高平均結果:加權平均在MySQL

select ContentType, AVG(Result) as AvgContent from dopractice where S_Id [email protected]_Id group by ContentType order by 2 desc limit 1; 

我想修改我的查詢來計算最高加權平均值。

例如:

((X*1)+(Y*2)+(Z*3))/3 

有沒有辦法做到這一點使用一個查詢?

注:dopractice表中的列(Id, StudentId, LessonId, ContentType, result, Date)

回答

-1

試試這個代碼。可能會對你有幫助。

select temp.content,MAX(temp.AvgContent) 
    FROM 
    (
    select ContentType AS content, AVG(Result) as AvgContent 
    from dopractice 
    where StudentId = @S_Id 
    group by ContentType order by ContentType desc 
    ) AS temp 
    group by temp.ContentType; 
+0

如何計算建議中的加權平均值? – John