Function
This statement is used to generate the cross-table row and achieve the cross-statistics of the GROUP BY field.
Syntax
| SELECT attr_expr_list FROM table_reference
GROUP BY col_name_list
GROUPING SETS(col_name_list);
|
Keyword
GROUPING SETS is the expansion of GROUP BY. For example:
Precautions
- The to-be-grouped table must exist. Otherwise, an error is reported.
- Different from ROLLUP, there is only one syntax for GROUPING SETS.
Example
To generate the cross-table row according to the group_id and job fields and return the total salary on each aggregation condition, run the following statement:
| SELECT group_id, job, SUM(salary) FROM group_test
GROUP BY group_id, job
GROUPING SETS (group_id, job);
|