GROUP BY-KlauselGROUP BY clause

Die- GROUP BY Klausel wird verwendet, um die Zeilen basierend auf einem Satz angegebener Gruppierungs Ausdrücke und Compute-Aggregationen für die Gruppe von Zeilen basierend auf mindestens einer angegebenen Aggregatfunktion zu gruppieren.The GROUP BY clause is used to group the rows based on a set of specified grouping expressions and compute aggregations on the group of rows based on one or more specified aggregate functions. Spark unterstützt auch erweiterte Aggregationen, um mehrere Aggregationen für denselben Eingabedaten Satz Satz über die GROUPING SETS Klauseln, und durchzuführen CUBE ROLLUP .Spark also supports advanced aggregations to do multiple aggregations for the same input record set via GROUPING SETS, CUBE, ROLLUP clauses. Wenn eine Filter Klausel an eine Aggregatfunktion angefügt wird, werden nur die übereinstimmenden Zeilen an diese Funktion übermittelt.When a FILTER clause is attached to an aggregate function, only the matching rows are passed to that function.

SyntaxSyntax

GROUP BY group_expression [ , group_expression [ , ... ] ]
    [ { WITH ROLLUP | WITH CUBE | GROUPING SETS (grouping_set [ , ...]) } ]

GROUP BY GROUPING SETS (grouping_set [ , ...])

Aggregatfunktionen werden als definiert.While aggregate functions are defined as

aggregate_name ( [ DISTINCT ] expression [ , ... ] ) [ FILTER ( WHERE boolean_expression ) ]

ParameterParameters

  • GROUPING SETSGROUPING SETS

    Gruppiert die Zeilen für jede Teilmenge der Ausdrücke, die in den Gruppierungs Sätzen angegeben sind.Groups the rows for each subset of the expressions specified in the grouping sets. Beispielsweise GROUP BY GROUPING SETS (warehouse, product) ist semantisch gleichwertig mit den Ergebnissen von GROUP BY warehouse und GROUP BY product .For example, GROUP BY GROUPING SETS (warehouse, product) is semantically equivalent to union of results of GROUP BY warehouse and GROUP BY product. Diese Klausel ist eine kurzzeile für einen, UNION ALL wobei jeder Teil des UNION ALL Operators eine Aggregation der in der-Klausel angegebenen Spalten ausführt GROUPING SETS .This clause is a shorthand for a UNION ALL where each leg of the UNION ALL operator performs aggregation of subset of the columns specified in the GROUPING SETS clause.

  • grouping_setgrouping_set

    Ein Gruppierungs Satz wird durch 0 (null) oder mehrere durch Trennzeichen getrennte Ausdrücke in Klammern angegeben.A grouping set is specified by zero or more comma-separated expressions in parentheses.

    Syntax:( [ expression [ , ... ] ] )Syntax: ( [ expression [ , ... ] ] )

  • grouping_expressiongrouping_expression

    Gibt die critieria an, anhand derer die Zeilen gruppiert werden.Specifies the critieria based on which the rows are grouped together. Die Gruppierung von Zeilen wird auf der Grundlage von Ergebnis Werten der Gruppierungs Ausdrücke ausgeführt.The grouping of rows is performed based on result values of the grouping expressions. Ein Gruppierungs Ausdruck kann ein Spaltenalias, eine Spaltenposition oder ein Ausdruck sein.A grouping expression may be a column alias, a column position or an expression.

  • ROLLUPROLLUP

    Gibt mehrere Ebenen von Aggregationen in einer einzelnen Anweisung an.Specifies multiple levels of aggregations in a single statement. Diese Klausel wird zum Berechnen von Aggregationen verwendet, die auf mehreren Gruppierungs Gruppen basieren.This clause is used to compute aggregations based on multiple grouping sets. ROLLUP ist eine Kurzwert für GROUPING SETS .ROLLUP is a shorthand for GROUPING SETS. GROUP BY warehouse, product WITH ROLLUP entspricht beispielsweise GROUP BY GROUPING SETS ((warehouse, product), (warehouse), ()).For example, GROUP BY warehouse, product WITH ROLLUP is equivalent to GROUP BY GROUPING SETS ((warehouse, product), (warehouse), ()). Die n-Elemente einer ROLLUP Spezifikation ergeben n + 1 GROUPING SETS .The N elements of a ROLLUP specification results in N+1 GROUPING SETS.

  • CubeCUBE

    CUBE die-Klausel wird zum Ausführen von Aggregationen verwendet, die auf der Kombination der Gruppierungs Spalten in der- GROUP BY KlauselCUBE clause is used to perform aggregations based on combination of grouping columns specified in the GROUP BY clause. CUBE ist eine Kurzwert für GROUPING SETS .CUBE is a shorthand for GROUPING SETS. GROUP BY warehouse, product WITH CUBE entspricht beispielsweise GROUP BY GROUPING SETS ((warehouse, product), (warehouse), (product), ()).For example, GROUP BY warehouse, product WITH CUBE is equivalent to GROUP BY GROUPING SETS ((warehouse, product), (warehouse), (product), ()). Die N-Elemente einer CUBE Spezifikation ergeben 2 ^ N GROUPING SETS .The N elements of a CUBE specification results in 2^N GROUPING SETS.

  • aggregate_nameaggregate_name

    Gibt einen Aggregat Funktionsnamen an (min, Max, count, Sum, AVG usw.).Specifies an aggregate function name (MIN, MAX, COUNT, SUM, AVG, etc.).

  • DISTINCTDISTINCT

    Entfernt Duplikate in Eingabezeilen, bevor Sie an Aggregatfunktionen übermittelt werden.Removes duplicates in input rows before they are passed to aggregate functions.

  • FILTERFILTER

    Filtert die Eingabezeilen, für die boolean_expression in der- WHERE Klausel true ausgewertet wird, an die Aggregatfunktion. andere Zeilen werden verworfen.Filters the input rows for which the boolean_expression in the WHERE clause evaluates to true are passed to the aggregate function; other rows are discarded.

BeispieleExamples

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);

-- Sum of quantity per dealership. Group by `id`.
SELECT id, sum(quantity) FROM dealer GROUP BY id ORDER BY id;
+---+-------------+
| id|sum(quantity)|
+---+-------------+
|100|           32|
|200|           33|
|300|           13|
+---+-------------+

-- Use column position in GROUP by clause.
SELECT id, sum(quantity) FROM dealer GROUP BY 1 ORDER BY 1;
+---+-------------+
| id|sum(quantity)|
+---+-------------+
|100|           32|
|200|           33|
|300|           13|
+---+-------------+

-- Multiple aggregations.
-- 1. Sum of quantity per dealership.
-- 2. Max quantity per dealership.
SELECT id, sum(quantity) AS sum, max(quantity) AS max FROM dealer GROUP BY id ORDER BY id;
+---+---+---+
| id|sum|max|
+---+---+---+
|100| 32| 15|
|200| 33| 20|
|300| 13|  8|
+---+---+---+

-- Count the number of distinct dealer cities per car_model.
SELECT car_model, count(DISTINCT city) AS count FROM dealer GROUP BY car_model;
+------------+-----+
|   car_model|count|
+------------+-----+
| Honda Civic|    3|
|   Honda CRV|    2|
|Honda Accord|    3|
+------------+-----+

-- Sum of only 'Honda Civic' and 'Honda CRV' quantities per dealership.
SELECT id, sum(quantity) FILTER (
            WHERE car_model IN ('Honda Civic', 'Honda CRV')
        ) AS `sum(quantity)` FROM dealer
    GROUP BY id ORDER BY id;
+---+-------------+
| id|sum(quantity)|
+---+-------------+
|100|           17|
|200|           23|
|300|            5|
+---+-------------+

-- Aggregations using multiple sets of grouping columns in a single statement.
-- Following performs aggregations based on four sets of grouping columns.
-- 1. city, car_model
-- 2. city
-- 3. car_model
-- 4. Empty grouping set. Returns quantities for all city and car models.
SELECT city, car_model, sum(quantity) AS sum FROM dealer
    GROUP BY GROUPING SETS ((city, car_model), (city), (car_model), ())
    ORDER BY city;
+---------+------------+---+
|     city|   car_model|sum|
+---------+------------+---+
|     null|        null| 78|
|     null| HondaAccord| 33|
|     null|    HondaCRV| 10|
|     null|  HondaCivic| 35|
|   Dublin|        null| 33|
|   Dublin| HondaAccord| 10|
|   Dublin|    HondaCRV|  3|
|   Dublin|  HondaCivic| 20|
|  Fremont|        null| 32|
|  Fremont| HondaAccord| 15|
|  Fremont|    HondaCRV|  7|
|  Fremont|  HondaCivic| 10|
| San Jose|        null| 13|
| San Jose| HondaAccord|  8|
| San Jose|  HondaCivic|  5|
+---------+------------+---+

-- Alternate syntax for `GROUPING SETS` in which both `GROUP BY` and `GROUPING SETS`
-- specifications are present.
SELECT city, car_model, sum(quantity) AS sum FROM dealer
    GROUP BY city, car_model GROUPING SETS ((city, car_model), (city), (car_model), ())
    ORDER BY city, car_model;
+---------+------------+---+
|     city|   car_model|sum|
+---------+------------+---+
|     null|        null| 78|
|     null| HondaAccord| 33|
|     null|    HondaCRV| 10|
|     null|  HondaCivic| 35|
|   Dublin|        null| 33|
|   Dublin| HondaAccord| 10|
|   Dublin|    HondaCRV|  3|
|   Dublin|  HondaCivic| 20|
|  Fremont|        null| 32|
|  Fremont| HondaAccord| 15|
|  Fremont|    HondaCRV|  7|
|  Fremont|  HondaCivic| 10|
| San Jose|        null| 13|
| San Jose| HondaAccord|  8|
| San Jose|  HondaCivic|  5|
+---------+------------+---+

-- Group by processing with `ROLLUP` clause.
-- Equivalent GROUP BY GROUPING SETS ((city, car_model), (city), ())
SELECT city, car_model, sum(quantity) AS sum FROM dealer
    GROUP BY city, car_model WITH ROLLUP
    ORDER BY city, car_model;
+---------+------------+---+
|     city|   car_model|sum|
+---------+------------+---+
|     null|        null| 78|
|   Dublin|        null| 33|
|   Dublin| HondaAccord| 10|
|   Dublin|    HondaCRV|  3|
|   Dublin|  HondaCivic| 20|
|  Fremont|        null| 32|
|  Fremont| HondaAccord| 15|
|  Fremont|    HondaCRV|  7|
|  Fremont|  HondaCivic| 10|
| San Jose|        null| 13|
| San Jose| HondaAccord|  8|
| San Jose|  HondaCivic|  5|
+---------+------------+---+

-- Group by processing with `CUBE` clause.
-- Equivalent GROUP BY GROUPING SETS ((city, car_model), (city), (car_model), ())
SELECT city, car_model, sum(quantity) AS sum FROM dealer
    GROUP BY city, car_model WITH CUBE
    ORDER BY city, car_model;
+---------+------------+---+
|     city|   car_model|sum|
+---------+------------+---+
|     null|        null| 78|
|     null| HondaAccord| 33|
|     null|    HondaCRV| 10|
|     null|  HondaCivic| 35|
|   Dublin|        null| 33|
|   Dublin| HondaAccord| 10|
|   Dublin|    HondaCRV|  3|
|   Dublin|  HondaCivic| 20|
|  Fremont|        null| 32|
|  Fremont| HondaAccord| 15|
|  Fremont|    HondaCRV|  7|
|  Fremont|  HondaCivic| 10|
| San Jose|        null| 13|
| San Jose| HondaAccord|  8|
| San Jose|  HondaCivic|  5|
+---------+------------+---+

--Prepare data for ignore nulls example
CREATE TABLE person (id INT, name STRING, age INT);
INSERT INTO person VALUES
    (100, 'Mary', NULL),
    (200, 'John', 30),
    (300, 'Mike', 80),
    (400, 'Dan', 50);

--Select the first row in cloumn age
SELECT FIRST(age) FROM person;
+--------------------+
| first(age, false)  |
+--------------------+
| NULL               |
+--------------------+

--Get the first row in cloumn `age` ignore nulls,last row in column `id` and sum of cloumn `id`.
SELECT FIRST(age IGNORE NULLS), LAST(id), SUM(id) FROM person;
+-------------------+------------------+----------+
| first(age, true)  | last(id, false)  | sum(id)  |
+-------------------+------------------+----------+
| 30                | 400              | 1000     |
+-------------------+------------------+----------+