GROUP BY Clause

Description

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. The grouping expressions and advanced aggregations can be mixed in the GROUP BY clause and nested in a GROUPING SETS clause. See more details in the Mixed/Nested Grouping Analytics section. When a FILTER clause is attached to an aggregate function, only the matching rows are passed to that function.

Syntax

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

While aggregate functions are defined as

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

Parameters

  • group_expression

    Specifies the criteria 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 name like GROUP BY a, a column position like GROUP BY 0, or an expression like GROUP BY a + b.

  • grouping_set

    A grouping set is specified by zero or more comma-separated expressions in parentheses. When the grouping set has only one element, parentheses can be omitted. For example, GROUPING SETS ((a), (b)) is the same as GROUPING SETS (a, b).

    Syntax: { ( [ expression [ , ... ] ] ) | expression }

  • GROUPING SETS

    Groups the rows for each grouping set specified after 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 each grouping set specified in the GROUPING SETS clause. Similarly, GROUP BY GROUPING SETS ((warehouse, product), (product), ()) is semantically equivalent to the union of results of GROUP BY warehouse, product, GROUP BY product and global aggregate.

    Note: For Hive compatibility Spark allows GROUP BY ... GROUPING SETS (...). The GROUP BY expressions are usually ignored, but if it contains extra expressions than the GROUPING SETS expressions, the extra expressions will be included in the grouping expressions and the value is always null. For example, SELECT a, b, c FROM ... GROUP BY a, b, c GROUPING SETS (a, b), the output of column c is always null.

  • 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 or GROUP BY ROLLUP(warehouse, product) is equivalent to GROUP BY GROUPING SETS((warehouse, product), (warehouse), ()). GROUP BY ROLLUP(warehouse, product, (warehouse, location)) is equivalent to GROUP BY GROUPING SETS((warehouse, product, location), (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 or GROUP BY CUBE(warehouse, product) is equivalent to GROUP BY GROUPING SETS((warehouse, product), (warehouse), (product), ()). GROUP BY CUBE(warehouse, product, (warehouse, location)) is equivalent to GROUP BY GROUPING SETS((warehouse, product, location), (warehouse, product), (warehouse, location), (product, warehouse, location), (warehouse), (product), (warehouse, product), ()). The N elements of a CUBE specification results in 2^N GROUPING SETS.

  • Mixed/Nested Grouping Analytics

    A GROUP BY clause can include multiple group_expressions and multiple CUBE|ROLLUP|GROUPING SETSs. GROUPING SETS can also have nested CUBE|ROLLUP|GROUPING SETS clauses, e.g. GROUPING SETS(ROLLUP(warehouse, location), CUBE(warehouse, location)), GROUPING SETS(warehouse, GROUPING SETS(location, GROUPING SETS(ROLLUP(warehouse, location), CUBE(warehouse, location)))). CUBE|ROLLUP is just a syntax sugar for GROUPING SETS, please refer to the sections above for how to translate CUBE|ROLLUP to GROUPING SETS. group_expression can be treated as a single-group GROUPING SETS under this context. For multiple GROUPING SETS in the GROUP BY clause, we generate a single GROUPING SETS by doing a cross-product of the original GROUPING SETSs. For nested GROUPING SETS in the GROUPING SETS clause, we simply take its grouping sets and strip it. For example, GROUP BY warehouse, GROUPING SETS((product), ()), GROUPING SETS((location, size), (location), (size), ()) and GROUP BY warehouse, ROLLUP(product), CUBE(location, size) is equivalent to GROUP BY GROUPING SETS( (warehouse, product, location, size), (warehouse, product, location), (warehouse, product, size), (warehouse, product), (warehouse, location, size), (warehouse, location), (warehouse, size), (warehouse)).

    GROUP BY GROUPING SETS(GROUPING SETS(warehouse), GROUPING SETS((warehouse, product))) is equivalent to GROUP BY GROUPING SETS((warehouse), (warehouse, product)).

  • aggregate_name

    Specifies an aggregate function name (MIN, MAX, COUNT, SUM, AVG, etc.).

  • DISTINCT

    Removes duplicates in input rows before they are passed to aggregate functions.

  • FILTER

    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.

Examples

  1. CREATE TABLE dealer (id INT, city STRING, car_model STRING, quantity INT);
  2. INSERT INTO dealer VALUES
  3. (100, 'Fremont', 'Honda Civic', 10),
  4. (100, 'Fremont', 'Honda Accord', 15),
  5. (100, 'Fremont', 'Honda CRV', 7),
  6. (200, 'Dublin', 'Honda Civic', 20),
  7. (200, 'Dublin', 'Honda Accord', 10),
  8. (200, 'Dublin', 'Honda CRV', 3),
  9. (300, 'San Jose', 'Honda Civic', 5),
  10. (300, 'San Jose', 'Honda Accord', 8);
  11. -- Sum of quantity per dealership. Group by `id`.
  12. SELECT id, sum(quantity) FROM dealer GROUP BY id ORDER BY id;
  13. +---+-------------+
  14. | id|sum(quantity)|
  15. +---+-------------+
  16. |100| 32|
  17. |200| 33|
  18. |300| 13|
  19. +---+-------------+
  20. -- Use column position in GROUP by clause.
  21. SELECT id, sum(quantity) FROM dealer GROUP BY 1 ORDER BY 1;
  22. +---+-------------+
  23. | id|sum(quantity)|
  24. +---+-------------+
  25. |100| 32|
  26. |200| 33|
  27. |300| 13|
  28. +---+-------------+
  29. -- Multiple aggregations.
  30. -- 1. Sum of quantity per dealership.
  31. -- 2. Max quantity per dealership.
  32. SELECT id, sum(quantity) AS sum, max(quantity) AS max FROM dealer GROUP BY id ORDER BY id;
  33. +---+---+---+
  34. | id|sum|max|
  35. +---+---+---+
  36. |100| 32| 15|
  37. |200| 33| 20|
  38. |300| 13| 8|
  39. +---+---+---+
  40. -- Count the number of distinct dealer cities per car_model.
  41. SELECT car_model, count(DISTINCT city) AS count FROM dealer GROUP BY car_model;
  42. +------------+-----+
  43. | car_model|count|
  44. +------------+-----+
  45. | Honda Civic| 3|
  46. | Honda CRV| 2|
  47. |Honda Accord| 3|
  48. +------------+-----+
  49. -- Sum of only 'Honda Civic' and 'Honda CRV' quantities per dealership.
  50. SELECT id, sum(quantity) FILTER (
  51. WHERE car_model IN ('Honda Civic', 'Honda CRV')
  52. ) AS `sum(quantity)` FROM dealer
  53. GROUP BY id ORDER BY id;
  54. +---+-------------+
  55. | id|sum(quantity)|
  56. +---+-------------+
  57. |100| 17|
  58. |200| 23|
  59. |300| 5|
  60. +---+-------------+
  61. -- Aggregations using multiple sets of grouping columns in a single statement.
  62. -- Following performs aggregations based on four sets of grouping columns.
  63. -- 1. city, car_model
  64. -- 2. city
  65. -- 3. car_model
  66. -- 4. Empty grouping set. Returns quantities for all city and car models.
  67. SELECT city, car_model, sum(quantity) AS sum FROM dealer
  68. GROUP BY GROUPING SETS ((city, car_model), (city), (car_model), ())
  69. ORDER BY city;
  70. +---------+------------+---+
  71. | city| car_model|sum|
  72. +---------+------------+---+
  73. | null| null| 78|
  74. | null| HondaAccord| 33|
  75. | null| HondaCRV| 10|
  76. | null| HondaCivic| 35|
  77. | Dublin| null| 33|
  78. | Dublin| HondaAccord| 10|
  79. | Dublin| HondaCRV| 3|
  80. | Dublin| HondaCivic| 20|
  81. | Fremont| null| 32|
  82. | Fremont| HondaAccord| 15|
  83. | Fremont| HondaCRV| 7|
  84. | Fremont| HondaCivic| 10|
  85. | San Jose| null| 13|
  86. | San Jose| HondaAccord| 8|
  87. | San Jose| HondaCivic| 5|
  88. +---------+------------+---+
  89. -- Group by processing with `ROLLUP` clause.
  90. -- Equivalent GROUP BY GROUPING SETS ((city, car_model), (city), ())
  91. SELECT city, car_model, sum(quantity) AS sum FROM dealer
  92. GROUP BY city, car_model WITH ROLLUP
  93. ORDER BY city, car_model;
  94. +---------+------------+---+
  95. | city| car_model|sum|
  96. +---------+------------+---+
  97. | null| null| 78|
  98. | Dublin| null| 33|
  99. | Dublin| HondaAccord| 10|
  100. | Dublin| HondaCRV| 3|
  101. | Dublin| HondaCivic| 20|
  102. | Fremont| null| 32|
  103. | Fremont| HondaAccord| 15|
  104. | Fremont| HondaCRV| 7|
  105. | Fremont| HondaCivic| 10|
  106. | San Jose| null| 13|
  107. | San Jose| HondaAccord| 8|
  108. | San Jose| HondaCivic| 5|
  109. +---------+------------+---+
  110. -- Group by processing with `CUBE` clause.
  111. -- Equivalent GROUP BY GROUPING SETS ((city, car_model), (city), (car_model), ())
  112. SELECT city, car_model, sum(quantity) AS sum FROM dealer
  113. GROUP BY city, car_model WITH CUBE
  114. ORDER BY city, car_model;
  115. +---------+------------+---+
  116. | city| car_model|sum|
  117. +---------+------------+---+
  118. | null| null| 78|
  119. | null| HondaAccord| 33|
  120. | null| HondaCRV| 10|
  121. | null| HondaCivic| 35|
  122. | Dublin| null| 33|
  123. | Dublin| HondaAccord| 10|
  124. | Dublin| HondaCRV| 3|
  125. | Dublin| HondaCivic| 20|
  126. | Fremont| null| 32|
  127. | Fremont| HondaAccord| 15|
  128. | Fremont| HondaCRV| 7|
  129. | Fremont| HondaCivic| 10|
  130. | San Jose| null| 13|
  131. | San Jose| HondaAccord| 8|
  132. | San Jose| HondaCivic| 5|
  133. +---------+------------+---+
  134. --Prepare data for ignore nulls example
  135. CREATE TABLE person (id INT, name STRING, age INT);
  136. INSERT INTO person VALUES
  137. (100, 'Mary', NULL),
  138. (200, 'John', 30),
  139. (300, 'Mike', 80),
  140. (400, 'Dan', 50);
  141. --Select the first row in column age
  142. SELECT FIRST(age) FROM person;
  143. +--------------------+
  144. | first(age, false) |
  145. +--------------------+
  146. | NULL |
  147. +--------------------+
  148. --Get the first row in column `age` ignore nulls,last row in column `id` and sum of column `id`.
  149. SELECT FIRST(age IGNORE NULLS), LAST(id), SUM(id) FROM person;
  150. +-------------------+------------------+----------+
  151. | first(age, true) | last(id, false) | sum(id) |
  152. +-------------------+------------------+----------+
  153. | 30 | 400 | 1000 |
  154. +-------------------+------------------+----------+