mysqlsqlsumaverageinner-query

How to write average innerQuery


Source Data

From the Image I have same course id and multiple videos for i want to show the overall watched percentage as the average of them how can I do that I want something like this:

(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 

Solution

  • As per comments, you'd get a weighted average this way

    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