我正在努力解决一个问题,但似乎无处可去。我想展示数学成绩低于平均水平的12年级学生,而不是显示他们的平均数字标记。
我正在使用msAccess并怀疑是否需要使用嵌套查询。我使用的字段是first_name,last_name,grade(从1到12)和Maths(包含数学标记)
我有这个:
Select first_name,last_name,maths
FROM students
WHERE grade = 12
HAVING ROUND(AVG(maths),1)< maths;
输出:
错误: 您试图执行不包含指定表达式的查询&#39; first_name&#39;作为聚合函数的一部分
然而,我不知道为什么它会抛出这个错误,即使从select中删除了我想要做的字段,因为我需要显示它
答案 0 :(得分:0)
要让得分低于平均水平的用户,您可以进行类似于您的查询,但使用group by
:
select s.student_id, avg(maths) as avg_maths
from students as s
where s.grade = 12
group by s.student_id
having avg(maths) < (select avg(maths) from students where grade = 12);
(注意:这假设您拥有每个学生的ID,而不是使用该名称。)
接下来,您可以通过各种方式获得原始数学分数。一种简单的方法是使用in
:
select first_name, last_name, maths
from students
where grade = 12 and
student_id in (select s.student_id, avg(s.maths) as avg_maths
from students as s
where s.grade = 12
group by s.student_id
having avg(maths) < (select avg(maths) from students where grade = 12)
);