有什么办法可以缩短这个查询吗?
Is there a way I can shorten this query?
我正在解决一个任务,我设法找到了解决方案,但我不确定这是否是编写此查询的最佳方式
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”没用)
为了清晰、测试和调试目的,您可以通过将相同的长表达式包含在子查询中来避免重复。例如:
select studentname,
case when av >= 80 then 'EXCELLENT'
when av >= 70 then 'VERY GOOD'
when av >= 60 then 'GOOD'
when av >= 50 then 'ACCEPTABLE'
else 'FAIL'
end as grade
from (
SELECT s.studentname,
(avg(cs.exam_season_one) + avg(cs.exam_season_two) +
avg(cs.degree_season_one) + avg(cs.degree_season_two)) / 4 as av
FROM courses_student cs
JOIN students c on s.student_id = cs.student_id
GROUP BY s.studentname
) x
它看起来并不短,但更容易发现错误并修复它。
您可以像这样尝试 CTE:
With CTE(
SELECT
students.studentname, 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 average
FROM
courses_student
JOIN
students
ON students.student_id = courses_student.student_id
GROUP BY
students.studentname
)
select
studentname,
case
when
average >= 80
then
'EXCELLENT'
when
average >= 70
then
'VERY GOOD'
when
average >= 60
then
'GOOD'
when
average >= 50
then
'ACCEPTABLE'
else
'FAIL'
end
as grade
from
CTE
我正在解决一个任务,我设法找到了解决方案,但我不确定这是否是编写此查询的最佳方式
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”没用)
为了清晰、测试和调试目的,您可以通过将相同的长表达式包含在子查询中来避免重复。例如:
select studentname,
case when av >= 80 then 'EXCELLENT'
when av >= 70 then 'VERY GOOD'
when av >= 60 then 'GOOD'
when av >= 50 then 'ACCEPTABLE'
else 'FAIL'
end as grade
from (
SELECT s.studentname,
(avg(cs.exam_season_one) + avg(cs.exam_season_two) +
avg(cs.degree_season_one) + avg(cs.degree_season_two)) / 4 as av
FROM courses_student cs
JOIN students c on s.student_id = cs.student_id
GROUP BY s.studentname
) x
它看起来并不短,但更容易发现错误并修复它。
您可以像这样尝试 CTE:
With CTE(
SELECT
students.studentname, 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 average
FROM
courses_student
JOIN
students
ON students.student_id = courses_student.student_id
GROUP BY
students.studentname
)
select
studentname,
case
when
average >= 80
then
'EXCELLENT'
when
average >= 70
then
'VERY GOOD'
when
average >= 60
then
'GOOD'
when
average >= 50
then
'ACCEPTABLE'
else
'FAIL'
end
as grade
from
CTE