-1
student(sid, sname, sex, age, year, gpa)
dept(dname, numphds)
prof(pname, dname)
course(cno, cname, dname)
major(dname, sid)
section(dname, cno, sectno, pname)
enroll(sid, grade, dname, cno, sectno)

select  e.cno, e.sectno, avg(s.gpa)
from enroll e, student s
where dname = 'Computer Science' and s.sid = e.sid
group by dname, cno, sectno

The result was:

I want to keep rows for each separate section(sectno) of the same class, but don't know how to get the average goal per class(the same cno) for the two sessions, displaying in two lines?

Thanks~

The result I want is

Any help would be appreciated~

2
  • yeah :) could you help me with that?
    – Echo0831
    Commented Jan 30, 2016 at 8:14
  • I've done what you said, but got the same result. did I get it wrong? select e.cno, e.sectno, avg(s.gpa) from enroll e join student s on s.sid = e.sid where dname = 'Computer Science' group by cno, sectno;
    – Echo0831
    Commented Jan 30, 2016 at 8:36

1 Answer 1

0

Try either

select tmp.course_number, sect.sectno as section_number, tmp.avg_gpa 
from (
    select  e.cno as course_number, avg(s.gpa) as avg_gpa
    from enroll e
    join student s on s.sid = e.sid
    where dname = 'Computer Science'  
    group by dname, cno
) as tmp
join section sect on sect.cno = tmp.course_number 
where sect.dname = 'Computer Science'

Or just remove sectno from your original group by clause and see what that gives you.

2
  • Your solution is great~BUT when I remove sectno, it throws an error says: column "e.sectno" must appear in the GROUP BY clause or be used in an aggregate function
    – Echo0831
    Commented Jan 30, 2016 at 9:39
  • if that's the case then you'll have to nest it anyway with something like select stuff.* from (your query here) as stuff group by dname, cno; so there's really not much difference between my first query and the second idea. Glad the first one worked for you :) Note that the second approach is only possible with MySQL as it has a different way of handling group by than other RDMS.
    – geco17
    Commented Jan 30, 2016 at 9:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.