出现 "not a single-group group function" 错误。所有非聚合列均已按以下方式列在组中
Getting a "not a single-group group function" error. All non-aggregate columns have been listed in the group by
SELECT STATUS,
AVG(COUNT(CRIME_ID)) "Avg # of Crimes per Status"
FROM CRIMES
GROUP BY STATUS;
当我尝试 运行 时,我得到“不是按功能分组的单组”。如您所见,我已将非聚合列包含在分组依据列表中。当它从 SELECT 列表中删除时,它 运行s,但我需要 运行 它。有什么问题?
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 实际上确实允许从其早期版本中嵌套两个聚合函数,但这样做意味着有两个聚合,第一个带有 GROUP BY 子句,而后一个没有。
这意味着我们不能 select 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.33333333333333333333333333333333333333
"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."
SELECT STATUS,
AVG(COUNT(CRIME_ID)) "Avg # of Crimes per Status"
FROM CRIMES
GROUP BY STATUS;
当我尝试 运行 时,我得到“不是按功能分组的单组”。如您所见,我已将非聚合列包含在分组依据列表中。当它从 SELECT 列表中删除时,它 运行s,但我需要 运行 它。有什么问题?
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 实际上确实允许从其早期版本中嵌套两个聚合函数,但这样做意味着有两个聚合,第一个带有 GROUP BY 子句,而后一个没有。
这意味着我们不能 select 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.33333333333333333333333333333333333333 |
"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))
`----------------
10925This calculation evaluates the inner aggregate (MAX(sal)) for each group defined by the GROUP BY clause (deptno), and aggregates the results again."