获得“不是单组的组函数”错误。所有非聚合列均已在分组依据中列出
SELECT STATUS,
AVG(COUNT(CRIME_ID)) "Avg # of Crimes per Status"
FROM CRIMES
GROUP BY STATUS;
当我尝试运行它时,我得到“不是按功能进行的单组分组”。正如您所看到的,我已将非聚合列包含在分组依据列表中。当它从 SELECT 列表中删除时,它会运行,但我需要用它来运行它。问题是什么?
SELECT STATUS,
AVG(COUNT(CRIME_ID)) "Avg # of Crimes per Status"
FROM CRIMES
GROUP BY STATUS;
When I try to run this I get the "not a single-group group by function". As you can see I have included the non-aggregated column in the group by list. When it is removed from the SELECT list it runs, but I need to run it with it. What is the problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Oracle 实际上确实允许在其早期版本中嵌套两个聚合函数,但这样做意味着存在两个聚合,第一个聚合带有 GROUP BY 子句,而后面的一个则没有。
这意味着我们无法选择 GROUP BY 中的列 -
这就是错误的根源。
通过从
SELECT
子句中删除STATUS
列,我们得到一个有效的查询:小提琴
Oracle9i SQL 参考版本 1 ( 9.0.1)
Oracle actually does allow for nesting two aggregate functions from its earlier versions, but doing so means that there are two aggregations, the first one with a GROUP BY clause but the one following it - without.
This means we cannot select the columns in the GROUP BY -
and that is the source of the error.
By removing the
STATUS
column from theSELECT
clause, we get a valid query:Fiddle
Oracle9i SQL Reference Release 1 (9.0.1)
在选择组时,Oracle 不允许以这种方式嵌套两个聚合函数。对计数查询进行子查询,然后取平均值:
请注意,您可以尝试:
这里我们使用
AVG()
作为结果集中所有组的分析函数。然后,我们将输出限制为一行。Oracle doesn't allow for nesting two aggregate functions in this way when also selecting the group. Subquery the count query and then take the average:
Note that you could try:
Here we are using
AVG()
as an analytic function over all groups in the result set. Then, we limit the output to just one row.