Skip to main content
Version: 3.2

grouping

Description

Indicates whether a column is an aggregate column. If it is an aggregate column, 0 is returned. Otherwise, 1 is returned.

Syntax

GROUPING(col_expr)

Parameters

col_expr: An expression for a dimension column specified in the expression-list of the ROLLUP, CUBE, or GROUPING SETS expansion of the GROUP BY clause.

Examples

MySQL > select * from t;
+------+------+
| k1 | k2 |
+------+------+
| NULL | B |
| NULL | b |
| A | NULL |
| A | B |
| A | b |
| a | NULL |
| a | B |
| a | b |
+------+------+
8 rows in set (0.12 sec)

MySQL > SELECT k1, k2, GROUPING(k1), GROUPING(k2), COUNT(*) FROM t GROUP BY ROLLUP(k1, k2);
+------+------+--------------+--------------+----------+
| k1 | k2 | grouping(k1) | grouping(k2) | count(*) |
+------+------+--------------+--------------+----------+
| NULL | NULL | 1 | 1 | 8 |
| NULL | NULL | 0 | 1 | 2 |
| NULL | B | 0 | 0 | 1 |
| NULL | b | 0 | 0 | 1 |
| A | NULL | 0 | 1 | 3 |
| a | NULL | 0 | 1 | 3 |
| A | NULL | 0 | 0 | 1 |
| A | B | 0 | 0 | 1 |
| A | b | 0 | 0 | 1 |
| a | NULL | 0 | 0 | 1 |
| a | B | 0 | 0 | 1 |
| a | b | 0 | 0 | 1 |
+------+------+--------------+--------------+----------+
12 rows in set (0.12 sec)