GROUPING

GROUPING indicates whether the values in a column in a GROUP BY list are aggregated or not. GROUPING returns a 1 if the values are aggregated in the return set or a 0 if they are not. GROUPING requires GROUP_BY and can be used only in the SELECT statement, the HAVING clause, or the ORDER BY clause.

ParameterTypeDescription
column
any
A column in a GROUP_BY clause.
Returns: integer

Example

query:

SELECT sales_agent,
       account,
       GROUPING(sales_agent),
       GROUPING(account),
       SUM(order_value)
  FROM apr_2017_orders
 GROUP BY ROLLUP(sales_agent, account)
 ORDER BY sales_agent, account

result: |sales_agent|account|grouping|grouping_2|sum| |-|-|-|-|-| |Boris Faz|Zencorporation|0|0|560| |Boris Faz|Zotware|0|0|59| |Boris Faz||0|1|18957| |Cassey Cress|Donquadtech|0|0|59| |Cassey Cress|Gekko & Co|0|0|1114| |Cassey Cress|Initech|0|0|3275|