【发布时间】:2022-12-02 21:34:51
【问题描述】:
我有下表:
Students (id, name, surname, study_year, department_id)
Courses(id, name)
Course_Signup(id, student_id, course_id, year)
Grades(signup_id, grade_type, mark, date),其中 grade_type 可以是“e”(考试)、“l”(实验室)或“p”(项目)
我想为每个学生显示考试和实验室+项目的平均成绩。
SELECT new_table.id,
new_table.name,
new_table.grade_type,
AVG(new_table.mark) AS "Average Exam Grade"
FROM (SELECT s.id, c.name, g.grade_type, g.mark
FROM Students s
JOIN Course_Signup csn
ON s.id = csn.student_id
JOIN Courses c
ON c.id = csn.course_id
JOIN Grades g
ON g.signup_id = csn.id) new_table
GROUP BY new_table.id, new_table.name, new_table.grade_type
HAVING new_table.grade_type = 'e'
ORDER BY new_table.id ASC
这将给我每个学生的平均考试成绩,对于他们注册的每门课程,但我还想有一个 AVG(new_table.mark) AS "Average Activity Grade",它将根据 grade_type = 'l' or grade_type = 'p' 列中的分数计算。由于我在HAVING中已经有了考试成绩条件,那么如何为第二个AVG添加第二个条件呢?
【问题讨论】:
标签: sql oracle join aggregate-functions