如何编写平均的innerQuery

时间:2017-05-09 05:07:37

标签: mysql sql sum average inner-query

Source Data

从图像中我有相同的课程ID和多个视频我想要显示总体观看百分比作为他们的平均值我该怎么做我想要这样的事情:

(SELECT SUM(watched_percentage) FROM tbl_student_learning_path where course_id = 298 
AND SELECT COUNT(watched_percentage) FROM tbl_student_learning_path where course_id = 298)
as overallScore 

2 个答案:

答案 0 :(得分:2)

根据评论,您可以通过这种方式获得加权平均值

SELECT
    course_id,
    (100 * SUM(watched_total_time) / SUM(video_total_time)) AS WeightedAvg
FROM
    tbl_student_learning_path 
WHERE
    course_id=298
GROUP BY
    course_id

答案 1 :(得分:1)

SELECT course_id,AVG(watched_percentage) AS Avg
FROM tbl_student_learning_path 
WHERE course_id=298
GROUP BY course_id