共用方式為


GROUP_BY_AGGREGATE 錯誤類別

SQLSTATE:42903

在 GROUP BY中不允許使用聚合函數,但在 <sqlExpr>中發現了。

參數

  • sqlExpr:包含聚合函數的運算式。

解釋

GROUP BY 子句的目的是識別一組不同的群組。 然後,每個數據列群組都會使用 SELECT 清單中的聚合函數折疊成單一數據列。 最後,您可以使用 HAVING 子句來篩選分組的數據列。

sqlExpr 位於 GROUP BY 子句中,而不是 SELECT 清單或 HAVING 子句。

緩解

錯誤的緩解取決於原因:

  • 您是否指定了正確的函式?

    sqlExpr 取代為不是聚合函數的適當函式。

  • 您要匯總運算式嗎?

    GROUP BY 移除表示式,並將它新增至 SELECT 清單。

  • 您要篩選匯總表達式嗎?

    GROUP BY 子句中移除表達式,並使用 BOOLEAN 運算符將其新增至 HAVING 子句。

例子

-- An aggregation in the GROUP BY clause
> SELECT a FROM VALUES(1, 2), (1, 3) AS T(a, b) GROUP BY a, sum(b);
 [GROUP_BY_AGGREGATE] Aggregate functions are not allowed in GROUP BY, but found sum(T.b).; line 1 pos 58;

-- Move the expression to the SELECT list
> SELECT a, sum(b) FROM VALUES(1, 2), (1, 3) AS T(a, b) GROUP BY a;
 1    5

-- An aggregation in the GROUP BY clause
> SELECT a, sum(b) FROM VALUES(1, 2), (1, 3), (2, 1) AS T(a, b) GROUP BY a, sum(b) > 1;
 [GROUP_BY_AGGREGATE] Aggregate functions are not allowed in GROUP BY, but found (sum(T.b) > CAST(1 AS BIGINT)).; line 1 pos 74;

-- Move the expression to the HAVING clause
> SELECT a, sum(b) FROM VALUES(1, 2), (1, 3), (2, 1) AS T(a, b) GROUP BY a HAVING sum(b) > 1;
 1    5