问题是:对于每个部门,列出他们提供的所有课程部分的最大和最小注册,只考虑那些有一些注册的部分 - 也就是说,不要担心那些没有学生参加的部分部分。
Department(dept_name, building, budget)
Section(course_id, sec_id, semester, year, building, room_number, time_slot_id)
student(student_id, name, major, tot_cred)
takes(student_id, course_id, sec_id, semester, year, grade)
course(course_id, title, dept_name, credits)
查询:
SELECT *
FROM course
NATURAL JOIN (SELECT count(student_id) AS tot
FROM takes NATURAL JOIN section GROUP BY course_id)
^^^给予注册。需要找到Max和Min
答案 0 :(得分:2)
您可以使用子查询:
select min(cnt), max(cnt)
from (select dept_name, count(*) as cnt
from student s join
takes t
on s.student_id = t.student_id join
course c
on t.course_id = c.course_id join
department d
on c.department_id = d.department_id
group by dept_name
) t
为此,我想象你有一个表course
,其中有一个部门ID。您上面的表格没有指向部门的链接。
您的查询根本没有意义。你不应该在FROM
子句中使用“,”。如果要进行交叉连接,请明确使用CROSS JOIN
。我希望您的查询在任何数据库中都有语法错误,因为enrollment
未定义。
答案 1 :(得分:0)
select
course_id,
min(tot_per_course_sec) min_section_enrolment,
max(tot_per_course_sec) max_section_enrolment
from course
natural join (
select course_id, sec_id, count(student_id) as tot_per_course_sec
from takes
natural join section
group by course_id, sec_id
having count(student_id) > 0
) sec_grouped
答案 2 :(得分:0)
对于您而言,您只需要关心表takes
。
这是我的方法:
SELECT MAX(enrollment) MaxEnrollment, MIN(enrollment) MinEnrollment
FROM (
SELECT COUNT(student_id) enrollment
FROM takes
GROUP BY course_id, sec_id, semester, year
) a;