Group By


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. Databricks SQL 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. (Databricks SQL Language Reference GROUP BY)

Groups rows with the same group-by-item expressions and computes aggregate functions for the resulting group. A GROUP BY expression can be:

  • A column name.

  • A number referencing a position in the SELECT list.

  • A general expression.

Extensions:

GROUP BY CUBE , GROUP BY GROUPING SETS , GROUP BY ROLLUP

(Snowflake SQL Language Reference GROUP BY)

Syntax

GROUP BY ALL

GROUP BY group_expression [, ...] [ WITH ROLLUP | WITH CUBE ]

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

grouping_set
   { expression |
     ( [ expression [, ...] ] ) }

Sample Source Patterns

Setup data

Databricks

Snowflake

Pattern code

Databricks

Snowflake

Known Issues

No issues were found

No related EWIs

Last updated

Was this helpful?