v.22.6New Feature

Add GROUPING Function for Disambiguation in ROLLUP, CUBE, and GROUPING SETS

Add GROUPING function. It allows to disambiguate the records in the queries with ROLLUP, CUBE or GROUPING SETS. Closes #19426. #37163 (Dmitry Novik).
Added the GROUPING function to ClickHouse, enabling distinction of rows produced by ROLLUP, CUBE, and GROUPING SETS operations.

Why it matters

The GROUPING function addresses the challenge of disambiguating aggregated rows generated by grouping extensions like ROLLUP, CUBE, and GROUPING SETS. This helps users to identify which columns are aggregated and which are present as is, improving analysis and clarity in complex grouped query results.

How to use it

Use the GROUPING function in your SELECT statements alongside ROLLUP, CUBE, or GROUPING SETS clauses. For example:

SELECT 
city,
country,
<code>GROUPING</code>(city) AS city_grouping,
<code>GROUPING</code>(country) AS country_grouping
FROM table_name
<code>GROUP BY</code> <code>ROLLUP</code>(city, country)

This will help identify which rows correspond to which grouping levels.