HAVING-Klausel
Gilt für: Databricks SQL Databricks Runtime
Filtert anhand der angegebenen Bedingung die von GROUP BY
erzeugten Ergebnisse. Wird häufig in Verbindung mit einer GROUP BY-Klausel verwendet.
Syntax
HAVING boolean_expression
Parameter
boolean_expression
Beliebiger Ausdruck, dessen Auswertung den Ergebnistyp
BOOLEAN
ergibt. Zwei oder mehr Ausdrücke können mithilfe von logischen Operatoren (z. B.AND
oderOR
) kombiniert werden.Die in der
HAVING
-Klausel angegebenen Ausdrücke können nur auf Folgendes verweisen:- Konstante Ausdrücke
- Ausdrücke, die in GROUP BY angezeigt werden
- Aggregatfunktionen
Beispiele
> CREATE TABLE dealer (id INT, city STRING, car_model STRING, quantity INT);
> INSERT INTO dealer VALUES
(100, 'Fremont' , 'Honda Civic' , 10),
(100, 'Fremont' , 'Honda Accord', 15),
(100, 'Fremont' , 'Honda CRV' , 7),
(200, 'Dublin' , 'Honda Civic' , 20),
(200, 'Dublin' , 'Honda Accord', 10),
(200, 'Dublin' , 'Honda CRV' , 3),
(300, 'San Jose', 'Honda Civic' , 5),
(300, 'San Jose', 'Honda Accord', 8);
-- `HAVING` clause referring to column in `GROUP BY`.
> SELECT city, sum(quantity) AS sum FROM dealer GROUP BY city HAVING city = 'Fremont';
Fremont 32
-- `HAVING` clause referring to aggregate function.
> SELECT city, sum(quantity) AS sum FROM dealer GROUP BY city HAVING sum(quantity) > 15;
Dublin 33
Fremont 32
-- `HAVING` clause referring to aggregate function by its alias.
> SELECT city, sum(quantity) AS sum FROM dealer GROUP BY city HAVING sum > 15;
Dublin 33
Fremont 32
-- `HAVING` clause referring to a different aggregate function than what is present in
-- `SELECT` list.
> SELECT city, sum(quantity) AS sum FROM dealer GROUP BY city HAVING max(quantity) > 15;
Dublin 33
-- `HAVING` clause referring to constant expression.
> SELECT city, sum(quantity) AS sum FROM dealer GROUP BY city HAVING 1 > 0 ORDER BY city;
Dublin 33
Fremont 32
San Jose 13
-- `HAVING` clause without a `GROUP BY` clause.
> SELECT sum(quantity) AS sum FROM dealer HAVING sum(quantity) > 10;
78