添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
SQLSTATE:42803

非聚合表达式 <expression> 基于不参与 GROUP BY 子句的列。

将列或表达式添加到 GROUP BY、聚合表达式,或者在不关心返回组中哪些值时使用 <expressionAnyValue>

表达式 SELECT 列表中的非聚合、非分组表达式。
  • expressionAnyValue:包装在 any_value() 聚合函数中的 expression
  • 在具有 GROUP BY 子句的查询上下文中, SELECT 列表中的本地列引用必须是:

  • 用作 聚合函数 的参数,或
  • GROUP BY 子句中的表达式匹配的表达式的一部分。
  • 局部列引用是已在查询的 FROM 子句中解析为 表引用 的列。

    换言之:列引用必须是分组键的一部分,或者必须是聚合的一部分。

    Azure Databricks 会尽力而为匹配表达式:例如,它将 识别为匹配的表达式 SELECT c1 + 5 FROM T GROUP BY 5 + c1 。 但 SELECT c1 FROM T GROUP BY c1 + 5 不是匹配项。

    错误的缓解取决于原因:

  • 是否缺少分组列?

    expression expression 的相关子表达式添加到 GROUP BY 子句。

  • 是否 是一个与 GROUP BY 不同的 epression 表达式中的列引用部分?

    匹配 SELECT 列表中的表达式或简化 GROUP BY 表达式。

  • 是否缺少聚合?

    使用聚合函数包装列引用。 如果只需要组中的代表性值,则可以使用 any_value(epression)

    -- Sample data
    > CREATE OR REPLACE TEMPORARY VIEW tasks(name, firstname, task, cost) AS
       VALUES ('Smith'  , 'Sam'   , 'UNPIVOT', 10),
              ('Smith'  , 'Sam'   , 'LATERAL',  5),
              ('Shuster', 'Sally' , 'DELETE' ,  7),
              ('Shuster', 'Sally' , 'GRANT'  ,  8);
    -- `name` and `firstname` are part of the group by coumns, but incomplete
    > SELECT name, firstname, sum(cost) FROM tasks GROUP BY firstname || ' ' || name;
     [MISSING_AGGREGATION] The expression "name" is neither present in the group by, nor is it an aggregate function.
    -- Match the GROUP BY expression
    > SELECT firstname || ' ' || name, sum(cost) FROM tasks GROUP BY firstname || ' ' || name;
     Sam Smith      15
     Sally Shuster  15
    -- Break up the GROUP BY expression
    > SELECT firstname, name, sum(cost) FROM tasks GROUP BY firstname, name;
     Sam    Smith   15
     Sally  Shuster 15
    -- Missing grouping column
    > SELECT name, firstname, sum(cost) FROM tasks GROUP BY name;
     [MISSING_AGGREGATION] The expression "firstname" is neither present in the group by, nor is it an aggregate function.
    -- Add the grouping column
    > SELECT firstname, name, sum(cost) FROM tasks GROUP BY firstname, name;
     Sam    Smith   15
     Sally  Shuster 15
    -- Missing aggregate
    > SELECT firstname, name, sum(cost), task FROM tasks GROUP BY firstname, name;
     [MISSING_AGGREGATION] The expression "task" is neither present in the group by, nor is it an aggregate function.
    -- Add an aggregate
    > SELECT firstname, name, sum(cost), array_agg(task) FROM tasks GROUP BY firstname, name;
     Sam    Smith   15  ["UNPIVOT","LATERAL"]
     Sally  Shuster 15  ["DELETE","GRANT"]
    -- Return any task
    > SELECT firstname, name, sum(cost), any_value(task) FROM tasks GROUP BY firstname, name;
     Sam    Smith   15  LATERAL
     Sally  Shuster 15  DELETE
                  GROUP BY 子句
  • HAVING 子句 SELECT(子选择)