classe di errore MISSING_AGGREGATION
L'espressione non di aggregazione <expression>
si basa su colonne che non fanno parte della clausola GROUP BY.
Aggiungi le colonne o l'espressione a GROUP BY, aggrega l'espressione, oppure usa <expressionAnyValue>
se non importa quale dei valori in un gruppo viene restituito.
Parametri
-
espressione: espressione non di aggregazione, non di raggruppamento nell'elenco di
SELECT
. -
expressionAnyValue:
expression
racchiuso in una funzione di aggregazione any_value().
Spiegazione
Nel contesto di una query con una clausola GROUP BY, i riferimenti alle colonne locali nell'elenco SELECT devono essere:
- Utilizzato come argomento per una funzione di aggregazione o
- Parte di un'espressione che corrisponde a un'espressione nella clausola
GROUP BY
.
Un riferimento a una colonna locale è una colonna che è stata risolta a un riferimento di tabella nella clausola FROM della query.
In altre parole: i riferimenti a colonne devono far parte delle chiavi di raggruppamento oppure devono far parte dell'aggregazione.
Azure Databricks corrisponde alle espressioni su : ad esempio riconoscerà: SELECT c1 + 5 FROM T GROUP BY 5 + c1
come espressioni matematiche.
Ma SELECT c1 FROM T GROUP BY c1 + 5
non è un abbinamento.
Mitigazione
La mitigazione dell'errore dipende dalla causa:
Hai perso una colonna di raggruppamento?
Aggiungere
expression
o la sottoespressione pertinente diexpression
alla clausolaGROUP BY
.Fa parte del riferimento di colonna di un'espressione
GROUP BY
che differisce daepression
?Trova l'espressione corrispondente nell'elenco
SELECT
o semplifica l'espressioneGROUP BY
.Manca l'aggregazione?
Avvolgere il riferimento alla colonna con una funzione di aggregazione. Se si vuole solo un valore rappresentativo del gruppo, è possibile usare any_value(epressione).
Esempi
-- 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