为了生成出勤率低于 75% 的学生 USN 列表以及出勤率和短缺的科目代码,当我运行以下查询时,我收到错误。我哪里做错了?你能纠正一下吗?
select attendance.usn,avg(attendance.ispresent),schedule.subcode
where avg(attendance.ispresent) in
(select avg(attendance.ispresent) having avg(attendance.ispresent)<0.75);
最佳答案
SELECT 都没有 FROM 子句。
内部查询使用 HAVING 子句,但这没有意义,因为没有 GROUP BY,因此它只会生成一条记录。
无论如何,我不确定 AVG 函数应该在这里完成什么。你想平均什么?您是否尝试使用 AVG 作为将 0 和 1 转换为百分比的棘手方法?如果是这样,ispresent 的定义是什么?如果它是 0 或 1 的 int,则 avg(ispresent) 将为 0,除非所有 ispresent 均为 1。
如果这有意义的话,您需要在外部选择上使用 GROUP BY。
也许你想要的是这样的:
select usn, subcode, avg(cast(ispresent as float))
from attendance
join schedule on ???
group by usn, subcode
having avg(cast(ispresent as float))<.75
关于mysql - 如何纠正错误 "invalid use of group function"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40740755/