GROUP_BY_AGGREGATE錯誤類別
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