2014-06-23 288 views
0

的百分比,我有以下表格:MySQL的 - 計算平均值

平均

id 
subject_id 
student_id 
value 
semester_id 

classes_students

id 
class_academic_year_id 
student_id 

classes_academic_years

id 
class_id 
academic_year_id 

學期

id 
name 
academic_year_id 
start_date 
end_date 

我需要一個查詢,找出從某一類學生的畢業率一學年的某個對象(即平均數高於5的那些人的百分比;例如假設學生的semester_id = 1的平均值爲6,semester_id = 2的平均值爲7,平均值的平均值爲6.5)。

這是當前查詢,但它只返回student_id和兩個學期的平均值。

select averages.student_id, avg(value) from averages 
inner join semesters on semesters.id = averages.semester_id 
inner join academic_years on academic_years.id = semesters.academic_year_id 

inner join classes_students on classes_students.student_id = averages.student_id 
inner join classes_academic_years on classes_academic_years.id = classes_students.class_academic_year_id 

where averages.subject_id = '66' 
and academic_years.id = 3 
and classes_academic_years.academic_year_id = academic_years.id 
and classes_academic_years.class_id = 259 

group by averages.student_id 

我想結果是類似於74.5%。有沒有辦法做到這一點?

回答

0

您應該使用該查詢並按類繼續加入。

SELECT COUNT(*) FROM classes_students GROUP BY class_academic_year_id ... 

這樣你就會有COUNT(已畢業)/ COUNT(*),這是0和1之間的數字,所以你只需要100乘它,你就會有你的百分比。