总和减去最小值的平均值
我有一个 SQL 语句,用于获取不同活动类型(家庭作业、测验等)的成绩,如果该类型的成绩下降到最低,则下降,否则保持不变。下面是错误以及 SQL 代码。
SELECT Student.firstName, Student.lastName, 'Grades' =
CASE
WHEN Grades.activityType = 'Homework' THEN
CASE WHEN Policy.drop_hw = 1 THEN
(AVG(SUM(Grades.grade) - MIN(Grades.grade))) * (Policy.homework / 100)
ELSE
(AVG(Grades.grade) * (Policy.homework / 100))
END
END, Course.courseNum, Course.sectNum, Grades.activityType
FROM ...
以下是我收到的错误:
- Cannot perform an aggregate function on an expression containing an aggregate or a subquery.
- Column 'Policy.drop_hw' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
I have an SQL statement that grabs the grades of different activity types (Homework, Quiz, etc), and if there's a drop lowest for that type, it drops, else, it remains. The errors are below as well as the SQL Code.
SELECT Student.firstName, Student.lastName, 'Grades' =
CASE
WHEN Grades.activityType = 'Homework' THEN
CASE WHEN Policy.drop_hw = 1 THEN
(AVG(SUM(Grades.grade) - MIN(Grades.grade))) * (Policy.homework / 100)
ELSE
(AVG(Grades.grade) * (Policy.homework / 100))
END
END, Course.courseNum, Course.sectNum, Grades.activityType
FROM ...
Here are the errors I'm getting:
- Cannot perform an aggregate function on an expression containing an aggregate or a subquery.
- Column 'Policy.drop_hw' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
研究分析函数。 (SO问题,Oracle 文档)。
像这样:
and:
根据您的情况设置分区;我们无法判断,因为您在示例中省略了
FROM ...
。然后,您可以在
CASE
语句内的任何计算中使用这些列别名。Look into analytical functions. (SO question, Oracle documentation).
Something like this:
and:
Set the partitioning with whatever makes sense in your case; we can't tell since you omitted the
FROM ...
in your example.You can then use those column aliases in any calculations inside your
CASE
statement.如果您只需要放弃一个最低成绩(在平局的情况下)
如果您需要放弃所有最低成绩:
If you only need to drop one lowest grade (in case of ties)
If you need to drop all lowest grades:
求和运算符给出一个结果(每组)。最小运算符也是如此。那么 avg-operator 应该聚合什么?
The sum-operator gives one result (per group). The min-operator, too. So over what should the avg-operator aggregate?