获得“不是单组的组函数”错误。所有非聚合列均已在分组依据中列出

发布于 2025-01-10 18:20:40 字数 314 浏览 0 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北笙凉宸 2025-01-17 18:20:40

Oracle 实际上确实允许在其早期版本中嵌套两个聚合函数,但这样做意味着存在两个聚合,第一个聚合带有 GROUP BY 子句,而后面的一个则没有。
这意味着我们无法选择 GROUP BY 中的列 -
这就是错误的根源。

通过从 SELECT 子句中删除 STATUS 列,我们得到一个有效的查询:

SELECT    AVG(COUNT(CRIME_ID)) "Avg # of Crimes per Status"
FROM      CRIMES
GROUP BY  STATUS;
Avg # of Crimes per Status
3.3333333333333333333333333333333333333

小提琴

可以嵌套聚合函数。例如以下示例
计算所有部门最高工资的平均值
在斯科特模式中:

从员工组中按部门 ID 选择 AVG(MAX(薪水));

平均(最高(工资))
`----------------
10925

此计算评估每个的内部聚合 (MAX(sal))
由 GROUP BY 子句 (deptno) 定义的组,并聚合
再次结果。”

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 the SELECT clause, we get a valid query:

SELECT    AVG(COUNT(CRIME_ID)) "Avg # of Crimes per Status"
FROM      CRIMES
GROUP BY  STATUS;
Avg # of Crimes per Status
3.33333333333333333333333333333333333333

Fiddle

"You can nest aggregate functions. For example, the following example
calculates the average of the maximum salaries of all the departments
in the scott schema:

SELECT AVG(MAX(salary)) FROM employees GROUP BY department_id;

AVG(MAX(SALARY))
`----------------
10925

This calculation evaluates the inner aggregate (MAX(sal)) for each
group defined by the GROUP BY clause (deptno), and aggregates the
results again."

Oracle9i SQL Reference Release 1 (9.0.1)

栖迟 2025-01-17 18:20:40

在选择组时,Oracle 不允许以这种方式嵌套两个聚合函数。对计数查询进行子查询,然后取平均值:

SELECT AVG(cnt)
FROM
(
    SELECT STATUS, COUNT(CRIME_ID) AS cnt
    FROM CRIMES
    GROUP BY STATUS
) t;

请注意,您可以尝试:

SELECT AVG(COUNT(CRIME_ID)) OVER () AS avg
FROM CRIMES
GROUP BY STATUS
ORDER BY STATUS
OFFSET 0 ROWS FETCH NEXT 1 ROWS ONLY;

这里我们使用 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:

SELECT AVG(cnt)
FROM
(
    SELECT STATUS, COUNT(CRIME_ID) AS cnt
    FROM CRIMES
    GROUP BY STATUS
) t;

Note that you could try:

SELECT AVG(COUNT(CRIME_ID)) OVER () AS avg
FROM CRIMES
GROUP BY STATUS
ORDER BY STATUS
OFFSET 0 ROWS FETCH NEXT 1 ROWS ONLY;

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文