【发布时间】:2022-01-05 06:59:03
【问题描述】:
我正在解决一项任务,我设法找到了解决方案,但我不确定这是否是编写此查询的最佳方式
SELECT
students.studentname,
CASE
WHEN (AVG(courses_student.exam_season_one) +
AVG(courses_student.exam_season_two) +
AVG(courses_student.degree_season_one) +
AVG(courses_student.degree_season_two)) / 4 >= 80
THEN 'EXCELLENT'
WHEN (AVG(courses_student.exam_season_one) +
AVG(courses_student.exam_season_two) +
AVG(courses_student.degree_season_one) +
AVG(courses_student.degree_season_two)) / 4 >= 70
THEN 'VERY GOOD'
WHEN (AVG(courses_student.exam_season_one) +
AVG(courses_student.exam_season_two) +
AVG(courses_student.degree_season_one) +
AVG(courses_student.degree_season_two)) / 4 >= 60
THEN 'GOOD'
WHEN (AVG(courses_student.exam_season_one) +
AVG(courses_student.exam_season_two) +
AVG(courses_student.degree_season_one) +
AVG(courses_student.degree_season_two)) / 4 >= 50
THEN 'ACCEPTABLE'
ELSE 'FAIL'
END AS GRADE
FROM
courses_student
JOIN
students ON students.student_id = courses_student.student_id
GROUP BY
students.studentname
如你所见,我重复了这个:
WHEN (AVG(courses_student.exam_season_one) +
AVG(courses_student.exam_season_two) +
AVG(courses_student.degree_season_one) +
AVG(courses_student.degree_season_two)) / 4
四次!而且它看起来很乱,所以有没有一种方法可以让它更短,比如只写一次,然后只使用一个单词? (我试过用“AS”还是不行)
【问题讨论】:
-
在一行中有多个考试分数这一事实证明了数据设计存在缺陷。严格按照第三范式设计您的表格,您的问题将不复存在。
标签: sql oracle oracle-sqldeveloper