Getting the above average student from database

后端 未结 5 1322
别跟我提以往
别跟我提以往 2021-01-15 18:40

I\'ve created a view that contains:

 student_full_name    subject_code    result
 Jennifer Higgins     CS1234          81
 Jennifer Higgins     CS1235                


        
5条回答
  •  失恋的感觉
    2021-01-15 19:15

    If you want to filter your results after aggregation (like avg), you need to use having rather than where.

    General rule is:

    • where filters what raw (non-aggregated) rows you get from the actual database before the aggregation takes place; and
    • having filters what (possibly aggregated) rows are finally delivered to you.

    Something like (though untested):

    SELECT   student_full_name,
             AVG (results) AS average_result
    FROM     viewEnrol
    GROUP BY student_full_name
    HAVING   AVG (results) > ( SELECT AVG (results) FROM viewEnrol )
    

提交回复
热议问题