Getting the above average student from database

2019-02-20 01:10发布

I've created a view that contains:

 student_full_name    subject_code    result
 Jennifer Higgins     CS1234          81
 Jennifer Higgins     CS1235          90
 Kal Penn             CS1234          70
 Kal Penn             CS1235          60
 Han Solo             CS1234          45
 Han Solo             CS1235          70

I am trying to get:

  1. Average result of each student so like say Jennifer Higgins enrolled in CS1234 and CS1235. Her average mark would be 85.50.

  2. Then Jennifer Higgins marks would be compared to the average mark of all enrolments So totalling up the AVG(result) for all subjects.

  3. The query would then list all the students who are getting above average scores.

I know I have to use a sub query here in order to get the AVG of all results. Well this is sort of a pseudo code. I am quite stuck as I'm not sure how to make the subquery compare itself to the query's results. I'm pretty sure I need two group by statements one for the grouping by student_full_name and the other to get all of the average results.

 SELECT student_full_name,
        AVG(results) AS average_result
 FROM viewEnrol
 WHERE average_result > ( SELECT (AVG(results))
                          FROM viewEnrol

 GROUP BY student_full_name

//EDIT

OUTPUT should look like. Kal Penn and Han Solo is not listed as they didn't get above average mark. Average mark of all subjects is 69.33. Han Solo got 57.5 and Kal Penn got 65.

 student_full_name    subject_code    result
 Jennifer Higgins     CS1234          85.5

Any help?

4条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-02-20 01:15

Every problem as a solution, I created a table

CREATE TABLE STUD
(SNAME varchar(20),
 SCODE NUMBER    PRIMARY KEY,
 MARKS NUMBER(4,2))

Inserted values

SNAME   SCODE   MARKS
--------------------
SAM     1001    90
VAS     1001    80
SAM     1002    60
ANAND   1001    80
VAS     1002    70
ANAND   1002    50

Query is

SELECT SNAME,AVG(MARKS) FROM STUD GROUP BY SNAME HAVING AVG(MARKS)>=
(SELECT MAX(MARKS ) FROM STUD WHERE SNAME='VAS')
查看更多
三岁会撩人
3楼-- · 2019-02-20 01:21

Getting the average result should just be a matter of filtering, so you shouldn't need to reference the outer query when calculating the global average. Your pseudo code is very close to what you need. You probably want to convert the where clause to a having clause. You also seem to be after comparing students that are above average students - not students who are performing above the net average. Make sure you're perfectly clear on what the definition of average is.

Something on this form:

select student_full_name, avg(results) as avg_result
from viewEnrol
group by student_full_name
having avg(results) > (
    select avg(avg_results)
    from (
        select name, avg(results) as avg_results
        group by student_full_name
        )
    )
查看更多
beautiful°
4楼-- · 2019-02-20 01:29

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 )
查看更多
闹够了就滚
5楼-- · 2019-02-20 01:40

This should do the job. First inner query will give you the result of average of all students. While second will give the avg for the table.

    SELECT student_full_name 
    FROM (SELECT student_full_name,
                 AVG(results) AS average_Sresult
          FROM viewEnrol
          GROUP BY student_full_name) sre, 
         (SELECT (AVG(results)) tavg
          FROM viewEnrol) ta
     WHERE sre.average_Sresult > ta.tavg

PS: Should not you take the avg of avg rather than taking the avg of all results.

查看更多
登录 后发表回答