GROUP BY
suggest changeIntroduction
A SELECT can group rows with GROUP BY. Rows that share the same values in the grouped columns form one group. Every selected column must either appear in the GROUP BY or be used inside an aggregate function. HAVING filters groups after aggregation; it does not define how values are aggregated. ROLLUP, CUBE, and GROUPING SETS are optional extensions of grouping. The bracket syntax below follows SQL Server.
Syntax
- GROUP BY {
column-expression
| ROLLUP ( <group_by_expression> [ ,…n ] )
| CUBE ( <group_by_expression> [ ,…n ] )
| GROUPING SETS ( [ ,…n ] )
| () –calculates the grand total
} [ ,…n ]
- <group_by_expression> ::=
column-expression
| ( column-expression [ ,…n ] )
- <grouping_set> ::=
() –calculates the grand total
| <grouping_set_item>
| ( <grouping_set_item> [ ,…n ] )
- <grouping_set_item> ::=
<group_by_expression>
| ROLLUP ( <group_by_expression> [ ,…n ] )
| CUBE ( <group_by_expression> [ ,…n ] )