spark-instrumented-optimizer/docs/sql-ref-syntax-qry-select-groupby.md
Wenchen Fan 0f0ccdadb1
[SPARK-31110][DOCS][SQL] refine sql doc for SELECT
### What changes were proposed in this pull request?

A few improvements to the sql ref SELECT doc:
1. correct the syntax of SELECT query
2. correct the default of null sort order
3. correct the GROUP BY syntax
4. several minor fixes

### Why are the changes needed?

refine document

### Does this PR introduce any user-facing change?

N/A

### How was this patch tested?

N/A

Closes #27866 from cloud-fan/doc.

Authored-by: Wenchen Fan <wenchen@databricks.com>
Signed-off-by: Dongjoon Hyun <dongjoon@apache.org>
2020-03-11 16:52:40 -07:00

8.7 KiB

layout title displayTitle license
global GROUP BY Clause GROUP BY Clause Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

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 also supports advanced aggregations to do multiple aggregations for the same input record set via GROUPING SETS, CUBE, ROLLUP clauses.

Syntax

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

GROUP BY GROUPING SETS (grouping_set [ , ...]) {% endhighlight %}

Parameters

GROUPING SETS
Groups the rows for each subset of the expressions specified in the grouping sets. For example, GROUP BY GROUPING SETS (warehouse, product) is semantically equivalent to union of results of GROUP BY warehouse and GROUP BY product. 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_set
A grouping set is specified by zero or more comma-separated expressions in parentheses.

Syntax: ([expression [, ...]])
grouping_expression
Specifies the critieria based on which the rows are grouped together. The grouping of rows is performed based on result values of the grouping expressions. A grouping expression may be a column alias, a column position or an expression.
ROLLUP
Specifies multiple levels of aggregations in a single statement. This clause is used to compute aggregations based on multiple grouping sets. ROLLUP is a shorthand for GROUPING SETS. For example, GROUP BY warehouse, product WITH ROLLUP is equivalent to GROUP BY GROUPING SETS ((warehouse, product), (warehouse), ()). The N elements of a ROLLUP specification results in N+1 GROUPING SETS.
CUBE
CUBE clause is used to perform aggregations based on combination of grouping columns specified in the GROUP BY clause. CUBE is a shorthand for GROUPING SETS. For example, GROUP BY warehouse, product WITH CUBE is equivalent to GROUP BY GROUPING SETS ((warehouse, product), (warehouse), (product), ()). The N elements of a CUBE specification results in 2^N GROUPING SETS.

Examples

{% highlight sql %} 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 | +---+---+---+

-- 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 |Honda Accord|33 | |null |Honda CRV |10 | |null |Honda Civic |35 | |Dublin |null |33 | |Dublin |Honda Accord|10 | |Dublin |Honda CRV |3 | |Dublin |Honda Civic |20 | |Fremont |null |32 | |Fremont |Honda Accord|15 | |Fremont |Honda CRV |7 | |Fremont |Honda Civic |10 | |San Jose|null |13 | |San Jose|Honda Accord|8 | |San Jose|Honda Civic |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 |Honda Accord|33 | |null |Honda CRV |10 | |null |Honda Civic |35 | |Dublin |null |33 | |Dublin |Honda Accord|10 | |Dublin |Honda CRV |3 | |Dublin |Honda Civic |20 | |Fremont |null |32 | |Fremont |Honda Accord|15 | |Fremont |Honda CRV |7 | |Fremont |Honda Civic |10 | |San Jose|null |13 | |San Jose|Honda Accord|8 | |San Jose|Honda Civic |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 |Honda Accord|10 | |Dublin |Honda CRV |3 | |Dublin |Honda Civic |20 | |Fremont |null |32 | |Fremont |Honda Accord|15 | |Fremont |Honda CRV |7 | |Fremont |Honda Civic |10 | |San Jose|null |13 | |San Jose|Honda Accord|8 | |San Jose|Honda Civic |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 |Honda Accord|33 | |null |Honda CRV |10 | |null |Honda Civic |35 | |Dublin |null |33 | |Dublin |Honda Accord|10 | |Dublin |Honda CRV |3 | |Dublin |Honda Civic |20 | |Fremont |null |32 | |Fremont |Honda Accord|15 | |Fremont |Honda CRV |7 | |Fremont |Honda Civic |10 | |San Jose|null |13 | |San Jose|Honda Accord|8 | |San Jose|Honda Civic |5 | +--------+------------+---+

{% endhighlight %}