select studentid,
sum(score) 'Total',
avg(IF(score>85)) 'Average',
max(score) 'Maximum',
min(score) 'Minimum'
from results
group by studentid;
错误消息:
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ')) 'Average',
max(score) 'Maximum',
最佳答案
您没有正确使用 if()
函数 - 并且您不应该使用单引号作为结果集中的列的别名。
您可能想要:
select
studentid,
sum(score) Total,
avg(score > 85) Average,
max(score) Maximum,
min(score) Minimum
from results
group by studentid;
avg(score > 85)
给出高于 85 的分数的比率,以 0
和 1
之间的十进制数表示。
另一方面,如果您希望平均分高于 85 分,您可以这样做:
avg(case when score > 85 then score end) Average
关于sql - 这段代码中的 IF 语句有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63674446/