πŸ•·οΈ Crawler Inspector

URL Lookup

Direct Parameter Lookup

Raw Queries and Responses

1. Shard Calculation

Query:
Response:
Calculated Shard: 89 (from laksa086)

2. Crawled Status Check

Query:
Response:

3. Robots.txt Check

Query:
Response:

4. Spam/Ban Check

Query:
Response:

5. Seen Status Check

ℹ️ Skipped - page is already crawled

πŸ“„
INDEXABLE
βœ…
CRAWLED
2 days ago
πŸ€–
ROBOTS ALLOWED

Page Info Filters

FilterStatusConditionDetails
HTTP statusPASSdownload_http_code = 200HTTP 200
Age cutoffPASSdownload_stamp > now() - 6 MONTH0.1 months ago
History dropPASSisNull(history_drop_reason)No drop reason
Spam/banPASSfh_dont_index != 1 AND ml_spam_score = 0ml_spam_score=0
CanonicalPASSmeta_canonical IS NULL OR = '' OR = src_unparsedNot set

Page Details

PropertyValue
URLhttps://clickhouse.com/docs/sql-reference/aggregate-functions/grouping_function
Last Crawled2026-04-12 07:34:47 (2 days ago)
First Indexed2025-02-21 02:43:43 (1 year ago)
HTTP Status Code200
Meta TitleGROUPING | ClickHouse Docs
Meta DescriptionDocumentation for the GROUPING aggregate function.
Meta Canonicalnull
Boilerpipe Text
GROUPING ​ ROLLUP and CUBE are modifiers to GROUP BY. Both of these calculate subtotals. ROLLUP takes an ordered list of columns, for example (day, month, year) , and calculates subtotals at each level of the aggregation and then a grand total. CUBE calculates subtotals across all possible combinations of the columns specified. GROUPING identifies which rows returned by ROLLUP or CUBE are superaggregates, and which are rows that would be returned by an unmodified GROUP BY. The GROUPING function takes multiple columns as an argument, and returns a bitmask. 1 indicates that a row returned by a ROLLUP or CUBE modifier to GROUP BY is a subtotal 0 indicates that a row returned by a ROLLUP or CUBE is a row that is not a subtotal GROUPING SETS ​ By default, the CUBE modifier calculates subtotals for all possible combinations of the columns passed to CUBE. GROUPING SETS allows you to specify the specific combinations to calculate. Analyzing hierarchical data is a good use case for ROLLUP, CUBE, and GROUPING SETS modifiers. The sample here is a table containing data about what Linux distribution, and the version of that distribution is installed across two datacenters. It may be valuable to look at the data by distribution, version, and location. Load sample data ​ CREATE TABLE servers ( datacenter VARCHAR ( 255 ) , distro VARCHAR ( 255 ) NOT NULL , version VARCHAR ( 50 ) NOT NULL , quantity INT ) ORDER BY ( datacenter , distro , version ) INSERT INTO servers ( datacenter , distro , version , quantity ) VALUES ( 'Schenectady' , 'Arch' , '2022.08.05' , 50 ) , ( 'Westport' , 'Arch' , '2022.08.05' , 40 ) , ( 'Schenectady' , 'Arch' , '2021.09.01' , 30 ) , ( 'Westport' , 'Arch' , '2021.09.01' , 20 ) , ( 'Schenectady' , 'Arch' , '2020.05.01' , 10 ) , ( 'Westport' , 'Arch' , '2020.05.01' , 5 ) , ( 'Schenectady' , 'RHEL' , '9' , 60 ) , ( 'Westport' , 'RHEL' , '9' , 70 ) , ( 'Westport' , 'RHEL' , '7' , 80 ) , ( 'Schenectady' , 'RHEL' , '7' , 80 ) SELECT * FROM servers ; β”Œβ”€datacenter──┬─distro─┬─version────┬─quantity─┐ β”‚ Schenectady β”‚ Arch β”‚ 2020.05.01 β”‚ 10 β”‚ β”‚ Schenectady β”‚ Arch β”‚ 2021.09.01 β”‚ 30 β”‚ β”‚ Schenectady β”‚ Arch β”‚ 2022.08.05 β”‚ 50 β”‚ β”‚ Schenectady β”‚ RHEL β”‚ 7 β”‚ 80 β”‚ β”‚ Schenectady β”‚ RHEL β”‚ 9 β”‚ 60 β”‚ β”‚ Westport β”‚ Arch β”‚ 2020.05.01 β”‚ 5 β”‚ β”‚ Westport β”‚ Arch β”‚ 2021.09.01 β”‚ 20 β”‚ β”‚ Westport β”‚ Arch β”‚ 2022.08.05 β”‚ 40 β”‚ β”‚ Westport β”‚ RHEL β”‚ 7 β”‚ 80 β”‚ β”‚ Westport β”‚ RHEL β”‚ 9 β”‚ 70 β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ 10 rows in set. Elapsed: 0.409 sec. Simple queries ​ Get the count of servers in each data center by distribution: SELECT datacenter , distro , SUM ( quantity ) qty FROM servers GROUP BY datacenter , distro ; β”Œβ”€datacenter──┬─distro─┬─qty─┐ β”‚ Schenectady β”‚ RHEL β”‚ 140 β”‚ β”‚ Westport β”‚ Arch β”‚ 65 β”‚ β”‚ Schenectady β”‚ Arch β”‚ 90 β”‚ β”‚ Westport β”‚ RHEL β”‚ 150 β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”˜ 4 rows in set. Elapsed: 0.212 sec. SELECT datacenter , SUM ( quantity ) qty FROM servers GROUP BY datacenter ; β”Œβ”€datacenter──┬─qty─┐ β”‚ Westport β”‚ 215 β”‚ β”‚ Schenectady β”‚ 230 β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”˜ 2 rows in set. Elapsed: 0.277 sec. SELECT distro , SUM ( quantity ) qty FROM servers GROUP BY distro ; β”Œβ”€distro─┬─qty─┐ β”‚ Arch β”‚ 155 β”‚ β”‚ RHEL β”‚ 290 β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”˜ 2 rows in set. Elapsed: 0.352 sec. SELECT SUM ( quantity ) qty FROM servers ; β”Œβ”€qty─┐ β”‚ 445 β”‚ β””β”€β”€β”€β”€β”€β”˜ 1 row in set. Elapsed: 0.244 sec. Comparing multiple GROUP BY statements with GROUPING SETS ​ Breaking down the data without CUBE, ROLLUP, or GROUPING SETS: SELECT datacenter , distro , SUM ( quantity ) qty FROM servers GROUP BY datacenter , distro UNION ALL SELECT datacenter , null , SUM ( quantity ) qty FROM servers GROUP BY datacenter UNION ALL SELECT null , distro , SUM ( quantity ) qty FROM servers GROUP BY distro UNION ALL SELECT null , null , SUM ( quantity ) qty FROM servers ; β”Œβ”€datacenter─┬─distro─┬─qty─┐ β”‚ ᴺᡁᴸᴸ β”‚ ᴺᡁᴸᴸ β”‚ 445 β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”˜ β”Œβ”€datacenter──┬─distro─┬─qty─┐ β”‚ Westport β”‚ ᴺᡁᴸᴸ β”‚ 215 β”‚ β”‚ Schenectady β”‚ ᴺᡁᴸᴸ β”‚ 230 β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”˜ β”Œβ”€datacenter──┬─distro─┬─qty─┐ β”‚ Schenectady β”‚ RHEL β”‚ 140 β”‚ β”‚ Westport β”‚ Arch β”‚ 65 β”‚ β”‚ Schenectady β”‚ Arch β”‚ 90 β”‚ β”‚ Westport β”‚ RHEL β”‚ 150 β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”˜ β”Œβ”€datacenter─┬─distro─┬─qty─┐ β”‚ ᴺᡁᴸᴸ β”‚ Arch β”‚ 155 β”‚ β”‚ ᴺᡁᴸᴸ β”‚ RHEL β”‚ 290 β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”˜ 9 rows in set. Elapsed: 0.527 sec. Getting the same information using GROUPING SETS: SELECT datacenter , distro , SUM ( quantity ) qty FROM servers GROUP BY GROUPING SETS ( ( datacenter , distro ) , ( datacenter ) , ( distro ) , ( ) ) β”Œβ”€datacenter──┬─distro─┬─qty─┐ β”‚ Schenectady β”‚ RHEL β”‚ 140 β”‚ β”‚ Westport β”‚ Arch β”‚ 65 β”‚ β”‚ Schenectady β”‚ Arch β”‚ 90 β”‚ β”‚ Westport β”‚ RHEL β”‚ 150 β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”˜ β”Œβ”€datacenter──┬─distro─┬─qty─┐ β”‚ Westport β”‚ β”‚ 215 β”‚ β”‚ Schenectady β”‚ β”‚ 230 β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”˜ β”Œβ”€datacenter─┬─distro─┬─qty─┐ β”‚ β”‚ β”‚ 445 β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”˜ β”Œβ”€datacenter─┬─distro─┬─qty─┐ β”‚ β”‚ Arch β”‚ 155 β”‚ β”‚ β”‚ RHEL β”‚ 290 β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”˜ 9 rows in set. Elapsed: 0.427 sec. Comparing CUBE with GROUPING SETS ​ The CUBE in the next query, CUBE(datacenter,distro,version) provides a hierarchy that may not make sense. It does not make sense to look at Version across the two distributions (as Arch and RHEL do not have the same release cycle or version naming standards). The GROUPING SETS example following this one is more appropriate as it groups distro and version in the same set. SELECT datacenter , distro , version , SUM ( quantity ) FROM servers GROUP BY CUBE ( datacenter , distro , version ) ORDER BY datacenter , distro ; β”Œβ”€datacenter──┬─distro─┬─version────┬─sum(quantity)─┐ β”‚ β”‚ β”‚ 7 β”‚ 160 β”‚ β”‚ β”‚ β”‚ 2020.05.01 β”‚ 15 β”‚ β”‚ β”‚ β”‚ 2021.09.01 β”‚ 50 β”‚ β”‚ β”‚ β”‚ 2022.08.05 β”‚ 90 β”‚ β”‚ β”‚ β”‚ 9 β”‚ 130 β”‚ β”‚ β”‚ β”‚ β”‚ 445 β”‚ β”‚ β”‚ Arch β”‚ 2021.09.01 β”‚ 50 β”‚ β”‚ β”‚ Arch β”‚ 2022.08.05 β”‚ 90 β”‚ β”‚ β”‚ Arch β”‚ 2020.05.01 β”‚ 15 β”‚ β”‚ β”‚ Arch β”‚ β”‚ 155 β”‚ β”‚ β”‚ RHEL β”‚ 9 β”‚ 130 β”‚ β”‚ β”‚ RHEL β”‚ 7 β”‚ 160 β”‚ β”‚ β”‚ RHEL β”‚ β”‚ 290 β”‚ β”‚ Schenectady β”‚ β”‚ 9 β”‚ 60 β”‚ β”‚ Schenectady β”‚ β”‚ 2021.09.01 β”‚ 30 β”‚ β”‚ Schenectady β”‚ β”‚ 7 β”‚ 80 β”‚ β”‚ Schenectady β”‚ β”‚ 2022.08.05 β”‚ 50 β”‚ β”‚ Schenectady β”‚ β”‚ 2020.05.01 β”‚ 10 β”‚ β”‚ Schenectady β”‚ β”‚ β”‚ 230 β”‚ β”‚ Schenectady β”‚ Arch β”‚ 2022.08.05 β”‚ 50 β”‚ β”‚ Schenectady β”‚ Arch β”‚ 2021.09.01 β”‚ 30 β”‚ β”‚ Schenectady β”‚ Arch β”‚ 2020.05.01 β”‚ 10 β”‚ β”‚ Schenectady β”‚ Arch β”‚ β”‚ 90 β”‚ β”‚ Schenectady β”‚ RHEL β”‚ 7 β”‚ 80 β”‚ β”‚ Schenectady β”‚ RHEL β”‚ 9 β”‚ 60 β”‚ β”‚ Schenectady β”‚ RHEL β”‚ β”‚ 140 β”‚ β”‚ Westport β”‚ β”‚ 9 β”‚ 70 β”‚ β”‚ Westport β”‚ β”‚ 2020.05.01 β”‚ 5 β”‚ β”‚ Westport β”‚ β”‚ 2022.08.05 β”‚ 40 β”‚ β”‚ Westport β”‚ β”‚ 7 β”‚ 80 β”‚ β”‚ Westport β”‚ β”‚ 2021.09.01 β”‚ 20 β”‚ β”‚ Westport β”‚ β”‚ β”‚ 215 β”‚ β”‚ Westport β”‚ Arch β”‚ 2020.05.01 β”‚ 5 β”‚ β”‚ Westport β”‚ Arch β”‚ 2021.09.01 β”‚ 20 β”‚ β”‚ Westport β”‚ Arch β”‚ 2022.08.05 β”‚ 40 β”‚ β”‚ Westport β”‚ Arch β”‚ β”‚ 65 β”‚ β”‚ Westport β”‚ RHEL β”‚ 9 β”‚ 70 β”‚ β”‚ Westport β”‚ RHEL β”‚ 7 β”‚ 80 β”‚ β”‚ Westport β”‚ RHEL β”‚ β”‚ 150 β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ 39 rows in set. Elapsed: 0.355 sec. Note Version in the above example may not make sense when it is not associated with a distro, if we were tracking the kernel version it might make sense because the kernel version can be associated with either distro. Using GROUPING SETS, as in the next example, may be a better choice. SELECT datacenter , distro , version , SUM ( quantity ) FROM servers GROUP BY GROUPING SETS ( ( datacenter , distro , version ) , ( datacenter , distro ) ) β”Œβ”€datacenter──┬─distro─┬─version────┬─sum(quantity)─┐ β”‚ Westport β”‚ RHEL β”‚ 9 β”‚ 70 β”‚ β”‚ Schenectady β”‚ Arch β”‚ 2022.08.05 β”‚ 50 β”‚ β”‚ Schenectady β”‚ Arch β”‚ 2021.09.01 β”‚ 30 β”‚ β”‚ Schenectady β”‚ RHEL β”‚ 7 β”‚ 80 β”‚ β”‚ Westport β”‚ Arch β”‚ 2020.05.01 β”‚ 5 β”‚ β”‚ Westport β”‚ RHEL β”‚ 7 β”‚ 80 β”‚ β”‚ Westport β”‚ Arch β”‚ 2021.09.01 β”‚ 20 β”‚ β”‚ Westport β”‚ Arch β”‚ 2022.08.05 β”‚ 40 β”‚ β”‚ Schenectady β”‚ RHEL β”‚ 9 β”‚ 60 β”‚ β”‚ Schenectady β”‚ Arch β”‚ 2020.05.01 β”‚ 10 β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”Œβ”€datacenter──┬─distro─┬─version─┬─sum(quantity)─┐ β”‚ Schenectady β”‚ RHEL β”‚ β”‚ 140 β”‚ β”‚ Westport β”‚ Arch β”‚ β”‚ 65 β”‚ β”‚ Schenectady β”‚ Arch β”‚ β”‚ 90 β”‚ β”‚ Westport β”‚ RHEL β”‚ β”‚ 150 β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ 14 rows in set. Elapsed: 1.036 sec.
Markdown
[Skip to main content](https://clickhouse.com/docs/sql-reference/aggregate-functions/grouping_function#__docusaurus_skipToContent_fallback) [![ClickHouse](https://clickhouse.com/docs/img/ch_logo_docs.svg)](https://clickhouse.com/) - [Products](https://clickhouse.com/docs/sql-reference/aggregate-functions/grouping_function) - [ClickHouse Cloud Best way to use ClickHouse. Available on AWS, GCP, and Azure.](https://clickhouse.com/cloud) - [BYOC (Bring Your Own Cloud) The fully managed ClickHouse Cloud service, Can be deployed in your AWS account.](https://clickhouse.com/cloud/bring-your-own-cloud) - [ClickHouse Set up a database with open-source ClickHouse. ClickHouse](https://clickhouse.com/clickhouse) - [Discover more than 100 integrations.](https://clickhouse.com/integrations) [Discover more than 100 integrations.](https://clickhouse.com/integrations) - [Use cases](https://clickhouse.com/docs/sql-reference/aggregate-functions/grouping_function) - [Real-time analytics](https://clickhouse.com/use-cases/real-time-analytics) - [Machine Learning & Generative AI](https://clickhouse.com/use-cases/machine-learning-and-data-science) - [Business Intelligence](https://clickhouse.com/use-cases/data-warehousing) - [Logs, Events, Traces](https://clickhouse.com/use-cases/observability) - [All use cases](https://clickhouse.com/use-cases) [All use cases](https://clickhouse.com/use-cases) - [Documentation](https://clickhouse.com/docs) - [Resources](https://clickhouse.com/docs/sql-reference/aggregate-functions/grouping_function) - [User stories](https://clickhouse.com/user-stories) - [Blog](https://clickhouse.com/blog) - [Events](https://clickhouse.com/company/events) - [Learning and certification](https://clickhouse.com/learn) - [Comparison](https://clickhouse.com/docs/sql-reference/aggregate-functions/grouping_function) - [BigQuery](https://clickhouse.com/comparison/bigquery) - [PostgreSQL](https://clickhouse.com/comparison/postgresql) - [Redshift](https://clickhouse.com/comparison/redshift) - [Rockset](https://clickhouse.com/comparison/rockset) - [Snowflake](https://clickhouse.com/comparison/snowflake) - [Video](https://clickhouse.com/videos) - [Demo](https://clickhouse.com/demos) - [Pricing](https://clickhouse.com/pricing) - [Contact](https://clickhouse.com/company/contact?loc=nav) [46\.8k](https://github.com/ClickHouse/ClickHouse?utm_source=clickhouse&utm_medium=website&utm_campaign=website-nav) [Search`Ctrl``K`](https://clickhouse.com/docs/search) [Sign in](https://console.clickhouse.cloud/signIn?loc=docs-nav-signIn-cta&glxid=e936910f-4374-41f2-9fe9-b48f22a26d3a&pagePath=%2Fdocs%2Fsql-reference%2Faggregate-functions%2Fgrouping_function&origPath=%2Fdocs%2Fsql-reference%2Faggregate-functions%2Fgrouping_function&utm_ga=GA1.1.1997658363.1775979289) [Get started](https://console.clickhouse.cloud/signUp?loc=docs-nav-signUp-cta&glxid=e936910f-4374-41f2-9fe9-b48f22a26d3a&pagePath=%2Fdocs%2Fsql-reference%2Faggregate-functions%2Fgrouping_function&origPath=%2Fdocs%2Fsql-reference%2Faggregate-functions%2Fgrouping_function&utm_ga=GA1.1.1997658363.1775979289) [Get started](https://clickhouse.com/docs/introduction-clickhouse) [Cloud](https://clickhouse.com/docs/cloud/overview) [Manage data](https://clickhouse.com/docs/updating-data) [Server admin](https://clickhouse.com/docs/guides/manage-and-deploy-index) [Reference](https://clickhouse.com/docs/sql-reference) [Integrations](https://clickhouse.com/docs/integrations) [ClickStack](https://clickhouse.com/docs/use-cases/observability/clickstack/overview) [chDB](https://clickhouse.com/docs/chdb) [About](https://clickhouse.com/docs/about) [Knowledge Base](https://clickhouse.com/docs/knowledgebase) [English](https://clickhouse.com/docs/sql-reference/aggregate-functions/grouping_function) - [English](https://clickhouse.com/docs/sql-reference/aggregate-functions/grouping_function) - [ζ—₯本θͺž](https://clickhouse.com/docs/jp/sql-reference/aggregate-functions/grouping_function) - [δΈ­ζ–‡](https://clickhouse.com/docs/zh/sql-reference/aggregate-functions/grouping_function) - [Русский](https://clickhouse.com/docs/ru/sql-reference/aggregate-functions/grouping_function) - [ν•œκ΅­μ–΄](https://clickhouse.com/docs/ko/sql-reference/aggregate-functions/grouping_function) [Skip to main content](https://clickhouse.com/docs/sql-reference/aggregate-functions/grouping_function#__docusaurus_skipToContent_fallback) [![ClickHouse](https://clickhouse.com/docs/img/ch_logo_docs.svg)](https://clickhouse.com/) - [Products](https://clickhouse.com/docs/sql-reference/aggregate-functions/grouping_function) - [ClickHouse Cloud Best way to use ClickHouse. Available on AWS, GCP, and Azure.](https://clickhouse.com/cloud) - [BYOC (Bring Your Own Cloud) The fully managed ClickHouse Cloud service, Can be deployed in your AWS account.](https://clickhouse.com/cloud/bring-your-own-cloud) - [ClickHouse Set up a database with open-source ClickHouse. ClickHouse](https://clickhouse.com/clickhouse) - [Discover more than 100 integrations.](https://clickhouse.com/integrations) [Discover more than 100 integrations.](https://clickhouse.com/integrations) - [Use cases](https://clickhouse.com/docs/sql-reference/aggregate-functions/grouping_function) - [Real-time analytics](https://clickhouse.com/use-cases/real-time-analytics) - [Machine Learning & Generative AI](https://clickhouse.com/use-cases/machine-learning-and-data-science) - [Business Intelligence](https://clickhouse.com/use-cases/data-warehousing) - [Logs, Events, Traces](https://clickhouse.com/use-cases/observability) - [All use cases](https://clickhouse.com/use-cases) [All use cases](https://clickhouse.com/use-cases) - [Documentation](https://clickhouse.com/docs) - [Resources](https://clickhouse.com/docs/sql-reference/aggregate-functions/grouping_function) - [User stories](https://clickhouse.com/user-stories) - [Blog](https://clickhouse.com/blog) - [Events](https://clickhouse.com/company/events) - [Learning and certification](https://clickhouse.com/learn) - [Comparison](https://clickhouse.com/docs/sql-reference/aggregate-functions/grouping_function) - [BigQuery](https://clickhouse.com/comparison/bigquery) - [PostgreSQL](https://clickhouse.com/comparison/postgresql) - [Redshift](https://clickhouse.com/comparison/redshift) - [Rockset](https://clickhouse.com/comparison/rockset) - [Snowflake](https://clickhouse.com/comparison/snowflake) - [Video](https://clickhouse.com/videos) - [Demo](https://clickhouse.com/demos) - [Pricing](https://clickhouse.com/pricing) - [Contact](https://clickhouse.com/company/contact?loc=nav) [46\.8k](https://github.com/ClickHouse/ClickHouse?utm_source=clickhouse&utm_medium=website&utm_campaign=website-nav) [Search`Ctrl``K`](https://clickhouse.com/docs/search) [Sign in](https://console.clickhouse.cloud/signIn?loc=docs-nav-signIn-cta&glxid=e936910f-4374-41f2-9fe9-b48f22a26d3a&pagePath=%2Fdocs%2Fsql-reference%2Faggregate-functions%2Fgrouping_function&origPath=%2Fdocs%2Fsql-reference%2Faggregate-functions%2Fgrouping_function&utm_ga=GA1.1.1997658363.1775979289) [Get started](https://console.clickhouse.cloud/signUp?loc=docs-nav-signUp-cta&glxid=e936910f-4374-41f2-9fe9-b48f22a26d3a&pagePath=%2Fdocs%2Fsql-reference%2Faggregate-functions%2Fgrouping_function&origPath=%2Fdocs%2Fsql-reference%2Faggregate-functions%2Fgrouping_function&utm_ga=GA1.1.1997658363.1775979289) [Get started](https://clickhouse.com/docs/introduction-clickhouse) [Cloud](https://clickhouse.com/docs/cloud/overview) [Manage data](https://clickhouse.com/docs/updating-data) [Server admin](https://clickhouse.com/docs/guides/manage-and-deploy-index) [Reference](https://clickhouse.com/docs/sql-reference) [Integrations](https://clickhouse.com/docs/integrations) [ClickStack](https://clickhouse.com/docs/use-cases/observability/clickstack/overview) [chDB](https://clickhouse.com/docs/chdb) [About](https://clickhouse.com/docs/about) [Knowledge Base](https://clickhouse.com/docs/knowledgebase) [English](https://clickhouse.com/docs/sql-reference/aggregate-functions/grouping_function) - [English](https://clickhouse.com/docs/sql-reference/aggregate-functions/grouping_function) - [ζ—₯本θͺž](https://clickhouse.com/docs/jp/sql-reference/aggregate-functions/grouping_function) - [δΈ­ζ–‡](https://clickhouse.com/docs/zh/sql-reference/aggregate-functions/grouping_function) - [Русский](https://clickhouse.com/docs/ru/sql-reference/aggregate-functions/grouping_function) - [ν•œκ΅­μ–΄](https://clickhouse.com/docs/ko/sql-reference/aggregate-functions/grouping_function) [Search`Ctrl``K`](https://clickhouse.com/docs/search) - [Introduction](https://clickhouse.com/docs/sql-reference) - [Syntax](https://clickhouse.com/docs/sql-reference/syntax) - [Input and Output Formats](https://clickhouse.com/docs/sql-reference/formats) - [Data types](https://clickhouse.com/docs/sql-reference/data-types) - [Statements](https://clickhouse.com/docs/sql-reference/statements) - [Operators](https://clickhouse.com/docs/sql-reference/operators) - [Engines](https://clickhouse.com/docs/engines) - [Database Engines](https://clickhouse.com/docs/engines/database-engines) - [Table Engines](https://clickhouse.com/docs/engines/table-engines) - [Functions](https://clickhouse.com/docs/sql-reference/functions) - [Regular functions](https://clickhouse.com/docs/sql-reference/functions/regular-functions) - [Aggregate functions](https://clickhouse.com/docs/sql-reference/aggregate-functions) - [Aggregate Functions](https://clickhouse.com/docs/sql-reference/aggregate-functions/reference) - [Combinators](https://clickhouse.com/docs/sql-reference/aggregate-functions/combinators) - [Parametric](https://clickhouse.com/docs/sql-reference/aggregate-functions/parametric-functions) - [GROUPING](https://clickhouse.com/docs/sql-reference/aggregate-functions/grouping_function) - [Combinator examples](https://clickhouse.com/docs/sql-reference/aggregate-functions/grouping_function) - [Table functions](https://clickhouse.com/docs/sql-reference/table-functions) - [Window functions](https://clickhouse.com/docs/sql-reference/window-functions) - [Formats](https://clickhouse.com/docs/interfaces/formats) - [Data Lakes](https://clickhouse.com/docs/sql-reference/datalakes) - [Functions](https://clickhouse.com/docs/sql-reference/functions) - [Aggregate functions](https://clickhouse.com/docs/sql-reference/aggregate-functions) - GROUPING [Edit this page](https://github.com/ClickHouse/ClickHouse/tree/master/docs/en/sql-reference/aggregate-functions/grouping_function.md) # GROUPING ## GROUPING[​](https://clickhouse.com/docs/sql-reference/aggregate-functions/grouping_function#grouping "Direct link to GROUPING") [ROLLUP](https://clickhouse.com/docs/sql-reference/statements/select/group-by#rollup-modifier) and [CUBE](https://clickhouse.com/docs/sql-reference/statements/select/group-by#cube-modifier) are modifiers to GROUP BY. Both of these calculate subtotals. ROLLUP takes an ordered list of columns, for example `(day, month, year)`, and calculates subtotals at each level of the aggregation and then a grand total. CUBE calculates subtotals across all possible combinations of the columns specified. GROUPING identifies which rows returned by ROLLUP or CUBE are superaggregates, and which are rows that would be returned by an unmodified GROUP BY. The GROUPING function takes multiple columns as an argument, and returns a bitmask. - `1` indicates that a row returned by a `ROLLUP` or `CUBE` modifier to `GROUP BY` is a subtotal - `0` indicates that a row returned by a `ROLLUP` or `CUBE` is a row that is not a subtotal ## GROUPING SETS[​](https://clickhouse.com/docs/sql-reference/aggregate-functions/grouping_function#grouping-sets "Direct link to GROUPING SETS") By default, the CUBE modifier calculates subtotals for all possible combinations of the columns passed to CUBE. GROUPING SETS allows you to specify the specific combinations to calculate. Analyzing hierarchical data is a good use case for ROLLUP, CUBE, and GROUPING SETS modifiers. The sample here is a table containing data about what Linux distribution, and the version of that distribution is installed across two datacenters. It may be valuable to look at the data by distribution, version, and location. ### Load sample data[​](https://clickhouse.com/docs/sql-reference/aggregate-functions/grouping_function#load-sample-data "Direct link to Load sample data") ``` CREATE TABLE servers ( datacenter VARCHAR(255), distro VARCHAR(255) NOT NULL, version VARCHAR(50) NOT NULL, quantity INT ) ORDER BY (datacenter, distro, version) ``` ``` INSERT INTO servers(datacenter, distro, version, quantity) VALUES ('Schenectady', 'Arch','2022.08.05',50), ('Westport', 'Arch','2022.08.05',40), ('Schenectady','Arch','2021.09.01',30), ('Westport', 'Arch','2021.09.01',20), ('Schenectady','Arch','2020.05.01',10), ('Westport', 'Arch','2020.05.01',5), ('Schenectady','RHEL','9',60), ('Westport','RHEL','9',70), ('Westport','RHEL','7',80), ('Schenectady','RHEL','7',80) ``` ``` SELECT * FROM servers; ``` ``` β”Œβ”€datacenter──┬─distro─┬─version────┬─quantity─┐ β”‚ Schenectady β”‚ Arch β”‚ 2020.05.01 β”‚ 10 β”‚ β”‚ Schenectady β”‚ Arch β”‚ 2021.09.01 β”‚ 30 β”‚ β”‚ Schenectady β”‚ Arch β”‚ 2022.08.05 β”‚ 50 β”‚ β”‚ Schenectady β”‚ RHEL β”‚ 7 β”‚ 80 β”‚ β”‚ Schenectady β”‚ RHEL β”‚ 9 β”‚ 60 β”‚ β”‚ Westport β”‚ Arch β”‚ 2020.05.01 β”‚ 5 β”‚ β”‚ Westport β”‚ Arch β”‚ 2021.09.01 β”‚ 20 β”‚ β”‚ Westport β”‚ Arch β”‚ 2022.08.05 β”‚ 40 β”‚ β”‚ Westport β”‚ RHEL β”‚ 7 β”‚ 80 β”‚ β”‚ Westport β”‚ RHEL β”‚ 9 β”‚ 70 β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ 10 rows in set. Elapsed: 0.409 sec. ``` ### Simple queries[​](https://clickhouse.com/docs/sql-reference/aggregate-functions/grouping_function#simple-queries "Direct link to Simple queries") Get the count of servers in each data center by distribution: ``` SELECT datacenter, distro, SUM (quantity) qty FROM servers GROUP BY datacenter, distro; ``` ``` β”Œβ”€datacenter──┬─distro─┬─qty─┐ β”‚ Schenectady β”‚ RHEL β”‚ 140 β”‚ β”‚ Westport β”‚ Arch β”‚ 65 β”‚ β”‚ Schenectady β”‚ Arch β”‚ 90 β”‚ β”‚ Westport β”‚ RHEL β”‚ 150 β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”˜ 4 rows in set. Elapsed: 0.212 sec. ``` ``` SELECT datacenter, SUM (quantity) qty FROM servers GROUP BY datacenter; ``` ``` β”Œβ”€datacenter──┬─qty─┐ β”‚ Westport β”‚ 215 β”‚ β”‚ Schenectady β”‚ 230 β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”˜ 2 rows in set. Elapsed: 0.277 sec. ``` ``` SELECT distro, SUM (quantity) qty FROM servers GROUP BY distro; ``` ``` β”Œβ”€distro─┬─qty─┐ β”‚ Arch β”‚ 155 β”‚ β”‚ RHEL β”‚ 290 β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”˜ 2 rows in set. Elapsed: 0.352 sec. ``` ``` SELECT SUM(quantity) qty FROM servers; ``` ``` β”Œβ”€qty─┐ β”‚ 445 β”‚ β””β”€β”€β”€β”€β”€β”˜ 1 row in set. Elapsed: 0.244 sec. ``` ### Comparing multiple GROUP BY statements with GROUPING SETS[​](https://clickhouse.com/docs/sql-reference/aggregate-functions/grouping_function#comparing-multiple-group-by-statements-with-grouping-sets "Direct link to Comparing multiple GROUP BY statements with GROUPING SETS") Breaking down the data without CUBE, ROLLUP, or GROUPING SETS: ``` SELECT datacenter, distro, SUM (quantity) qty FROM servers GROUP BY datacenter, distro UNION ALL SELECT datacenter, null, SUM (quantity) qty FROM servers GROUP BY datacenter UNION ALL SELECT null, distro, SUM (quantity) qty FROM servers GROUP BY distro UNION ALL SELECT null, null, SUM(quantity) qty FROM servers; ``` ``` β”Œβ”€datacenter─┬─distro─┬─qty─┐ β”‚ ᴺᡁᴸᴸ β”‚ ᴺᡁᴸᴸ β”‚ 445 β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”˜ β”Œβ”€datacenter──┬─distro─┬─qty─┐ β”‚ Westport β”‚ ᴺᡁᴸᴸ β”‚ 215 β”‚ β”‚ Schenectady β”‚ ᴺᡁᴸᴸ β”‚ 230 β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”˜ β”Œβ”€datacenter──┬─distro─┬─qty─┐ β”‚ Schenectady β”‚ RHEL β”‚ 140 β”‚ β”‚ Westport β”‚ Arch β”‚ 65 β”‚ β”‚ Schenectady β”‚ Arch β”‚ 90 β”‚ β”‚ Westport β”‚ RHEL β”‚ 150 β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”˜ β”Œβ”€datacenter─┬─distro─┬─qty─┐ β”‚ ᴺᡁᴸᴸ β”‚ Arch β”‚ 155 β”‚ β”‚ ᴺᡁᴸᴸ β”‚ RHEL β”‚ 290 β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”˜ 9 rows in set. Elapsed: 0.527 sec. ``` Getting the same information using GROUPING SETS: ``` SELECT datacenter, distro, SUM (quantity) qty FROM servers GROUP BY GROUPING SETS( (datacenter,distro), (datacenter), (distro), () ) ``` ``` β”Œβ”€datacenter──┬─distro─┬─qty─┐ β”‚ Schenectady β”‚ RHEL β”‚ 140 β”‚ β”‚ Westport β”‚ Arch β”‚ 65 β”‚ β”‚ Schenectady β”‚ Arch β”‚ 90 β”‚ β”‚ Westport β”‚ RHEL β”‚ 150 β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”˜ β”Œβ”€datacenter──┬─distro─┬─qty─┐ β”‚ Westport β”‚ β”‚ 215 β”‚ β”‚ Schenectady β”‚ β”‚ 230 β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”˜ β”Œβ”€datacenter─┬─distro─┬─qty─┐ β”‚ β”‚ β”‚ 445 β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”˜ β”Œβ”€datacenter─┬─distro─┬─qty─┐ β”‚ β”‚ Arch β”‚ 155 β”‚ β”‚ β”‚ RHEL β”‚ 290 β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”˜ 9 rows in set. Elapsed: 0.427 sec. ``` ### Comparing CUBE with GROUPING SETS[​](https://clickhouse.com/docs/sql-reference/aggregate-functions/grouping_function#comparing-cube-with-grouping-sets "Direct link to Comparing CUBE with GROUPING SETS") The CUBE in the next query, `CUBE(datacenter,distro,version)` provides a hierarchy that may not make sense. It does not make sense to look at Version across the two distributions (as Arch and RHEL do not have the same release cycle or version naming standards). The GROUPING SETS example following this one is more appropriate as it groups `distro` and `version` in the same set. ``` SELECT datacenter, distro, version, SUM(quantity) FROM servers GROUP BY CUBE(datacenter,distro,version) ORDER BY datacenter, distro; ``` ``` β”Œβ”€datacenter──┬─distro─┬─version────┬─sum(quantity)─┐ β”‚ β”‚ β”‚ 7 β”‚ 160 β”‚ β”‚ β”‚ β”‚ 2020.05.01 β”‚ 15 β”‚ β”‚ β”‚ β”‚ 2021.09.01 β”‚ 50 β”‚ β”‚ β”‚ β”‚ 2022.08.05 β”‚ 90 β”‚ β”‚ β”‚ β”‚ 9 β”‚ 130 β”‚ β”‚ β”‚ β”‚ β”‚ 445 β”‚ β”‚ β”‚ Arch β”‚ 2021.09.01 β”‚ 50 β”‚ β”‚ β”‚ Arch β”‚ 2022.08.05 β”‚ 90 β”‚ β”‚ β”‚ Arch β”‚ 2020.05.01 β”‚ 15 β”‚ β”‚ β”‚ Arch β”‚ β”‚ 155 β”‚ β”‚ β”‚ RHEL β”‚ 9 β”‚ 130 β”‚ β”‚ β”‚ RHEL β”‚ 7 β”‚ 160 β”‚ β”‚ β”‚ RHEL β”‚ β”‚ 290 β”‚ β”‚ Schenectady β”‚ β”‚ 9 β”‚ 60 β”‚ β”‚ Schenectady β”‚ β”‚ 2021.09.01 β”‚ 30 β”‚ β”‚ Schenectady β”‚ β”‚ 7 β”‚ 80 β”‚ β”‚ Schenectady β”‚ β”‚ 2022.08.05 β”‚ 50 β”‚ β”‚ Schenectady β”‚ β”‚ 2020.05.01 β”‚ 10 β”‚ β”‚ Schenectady β”‚ β”‚ β”‚ 230 β”‚ β”‚ Schenectady β”‚ Arch β”‚ 2022.08.05 β”‚ 50 β”‚ β”‚ Schenectady β”‚ Arch β”‚ 2021.09.01 β”‚ 30 β”‚ β”‚ Schenectady β”‚ Arch β”‚ 2020.05.01 β”‚ 10 β”‚ β”‚ Schenectady β”‚ Arch β”‚ β”‚ 90 β”‚ β”‚ Schenectady β”‚ RHEL β”‚ 7 β”‚ 80 β”‚ β”‚ Schenectady β”‚ RHEL β”‚ 9 β”‚ 60 β”‚ β”‚ Schenectady β”‚ RHEL β”‚ β”‚ 140 β”‚ β”‚ Westport β”‚ β”‚ 9 β”‚ 70 β”‚ β”‚ Westport β”‚ β”‚ 2020.05.01 β”‚ 5 β”‚ β”‚ Westport β”‚ β”‚ 2022.08.05 β”‚ 40 β”‚ β”‚ Westport β”‚ β”‚ 7 β”‚ 80 β”‚ β”‚ Westport β”‚ β”‚ 2021.09.01 β”‚ 20 β”‚ β”‚ Westport β”‚ β”‚ β”‚ 215 β”‚ β”‚ Westport β”‚ Arch β”‚ 2020.05.01 β”‚ 5 β”‚ β”‚ Westport β”‚ Arch β”‚ 2021.09.01 β”‚ 20 β”‚ β”‚ Westport β”‚ Arch β”‚ 2022.08.05 β”‚ 40 β”‚ β”‚ Westport β”‚ Arch β”‚ β”‚ 65 β”‚ β”‚ Westport β”‚ RHEL β”‚ 9 β”‚ 70 β”‚ β”‚ Westport β”‚ RHEL β”‚ 7 β”‚ 80 β”‚ β”‚ Westport β”‚ RHEL β”‚ β”‚ 150 β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ 39 rows in set. Elapsed: 0.355 sec. ``` Note Version in the above example may not make sense when it is not associated with a distro, if we were tracking the kernel version it might make sense because the kernel version can be associated with either distro. Using GROUPING SETS, as in the next example, may be a better choice. ``` SELECT datacenter, distro, version, SUM(quantity) FROM servers GROUP BY GROUPING SETS ( (datacenter, distro, version), (datacenter, distro)) ``` ``` β”Œβ”€datacenter──┬─distro─┬─version────┬─sum(quantity)─┐ β”‚ Westport β”‚ RHEL β”‚ 9 β”‚ 70 β”‚ β”‚ Schenectady β”‚ Arch β”‚ 2022.08.05 β”‚ 50 β”‚ β”‚ Schenectady β”‚ Arch β”‚ 2021.09.01 β”‚ 30 β”‚ β”‚ Schenectady β”‚ RHEL β”‚ 7 β”‚ 80 β”‚ β”‚ Westport β”‚ Arch β”‚ 2020.05.01 β”‚ 5 β”‚ β”‚ Westport β”‚ RHEL β”‚ 7 β”‚ 80 β”‚ β”‚ Westport β”‚ Arch β”‚ 2021.09.01 β”‚ 20 β”‚ β”‚ Westport β”‚ Arch β”‚ 2022.08.05 β”‚ 40 β”‚ β”‚ Schenectady β”‚ RHEL β”‚ 9 β”‚ 60 β”‚ β”‚ Schenectady β”‚ Arch β”‚ 2020.05.01 β”‚ 10 β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”Œβ”€datacenter──┬─distro─┬─version─┬─sum(quantity)─┐ β”‚ Schenectady β”‚ RHEL β”‚ β”‚ 140 β”‚ β”‚ Westport β”‚ Arch β”‚ β”‚ 65 β”‚ β”‚ Schenectady β”‚ Arch β”‚ β”‚ 90 β”‚ β”‚ Westport β”‚ RHEL β”‚ β”‚ 150 β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ 14 rows in set. Elapsed: 1.036 sec. ``` [Previous Parametric](https://clickhouse.com/docs/sql-reference/aggregate-functions/parametric-functions) [Next anyIf](https://clickhouse.com/docs/examples/aggregate-function-combinators/anyIf) - [GROUPING](https://clickhouse.com/docs/sql-reference/aggregate-functions/grouping_function#grouping) - [GROUPING SETS](https://clickhouse.com/docs/sql-reference/aggregate-functions/grouping_function#grouping-sets) - [Load sample data](https://clickhouse.com/docs/sql-reference/aggregate-functions/grouping_function#load-sample-data) - [Simple queries](https://clickhouse.com/docs/sql-reference/aggregate-functions/grouping_function#simple-queries) - [Comparing multiple GROUP BY statements with GROUPING SETS](https://clickhouse.com/docs/sql-reference/aggregate-functions/grouping_function#comparing-multiple-group-by-statements-with-grouping-sets) - [Comparing CUBE with GROUPING SETS](https://clickhouse.com/docs/sql-reference/aggregate-functions/grouping_function#comparing-cube-with-grouping-sets) Was this page helpful? ###### Try ClickHouse Cloud for FREE Separation of storage and compute, automatic scaling, built-in SQL console, and lots more. \$300 in free credits when signing up. [Try it for Free](https://console.clickhouse.cloud/signUp?loc=doc-card-banner&glxid=e936910f-4374-41f2-9fe9-b48f22a26d3a&pagePath=%2Fdocs%2Fsql-reference%2Faggregate-functions%2Fgrouping_function&origPath=%2Fdocs%2Fsql-reference%2Faggregate-functions%2Fgrouping_function&utm_ga=GA1.1.1997658363.1775979289) Β© 2016–2026 ClickHouse, Inc. [Trademark](https://clickhouse.com/legal/trademark-policy)Β·[Privacy](https://clickhouse.com/legal/privacy-policy)Β·[Security](https://trust.clickhouse.com/)Β·[Terms of Service](https://clickhouse.com/legal/agreements/terms-of-service) ![](https://static.scarf.sh/a.png?x-pxid=e6377503-591b-4886-9398-e69c7fee0b91) Β© 2016–2026 ClickHouse, Inc. [Trademark](https://clickhouse.com/legal/trademark-policy)Β·[Privacy](https://clickhouse.com/legal/privacy-policy)Β·[Security](https://trust.clickhouse.com/)Β·[Terms of Service](https://clickhouse.com/legal/agreements/terms-of-service) ![](https://static.scarf.sh/a.png?x-pxid=e6377503-591b-4886-9398-e69c7fee0b91) [![ClickHouse](https://clickhouse.com/docs/img/ch_logo_docs.svg)](https://clickhouse.com/) EN - Get startedβ–Ό - Cloudβ–Ό - Manage dataβ–Ό - Server adminβ–Ό - Referenceβ–Ό - Integrationsβ–Ό - ClickStackβ–Ό - chDBβ–Ό - Aboutβ–Ό [![ClickHouse](https://clickhouse.com/docs/img/ch_logo_docs.svg)](https://clickhouse.com/) EN main-menu - Introductionβ–Ό - [Syntax](https://clickhouse.com/docs/sql-reference/syntax) - [Input and Output Formats](https://clickhouse.com/docs/sql-reference/formats) - Data typesβ–Ό - Statementsβ–Ό - Operatorsβ–Ό - Enginesβ–Ό - Functionsβ–Ό - Regular functionsβ–Ό - Aggregate functionsβ–Ό - Aggregate Functionsβ–Ό - [Combinators](https://clickhouse.com/docs/sql-reference/aggregate-functions/combinators) - [Parametric](https://clickhouse.com/docs/sql-reference/aggregate-functions/parametric-functions) - [GROUPING](https://clickhouse.com/docs/sql-reference/aggregate-functions/grouping_function) - Combinator examplesβ–Ό - Table functionsβ–Ό - Window functionsβ–Ό - Formatsβ–Ό - [Data Lakes](https://clickhouse.com/docs/sql-reference/datalakes)
Readable Markdown
## GROUPING[​](https://clickhouse.com/docs/sql-reference/aggregate-functions/grouping_function#grouping "Direct link to GROUPING") [ROLLUP](https://clickhouse.com/docs/sql-reference/statements/select/group-by#rollup-modifier) and [CUBE](https://clickhouse.com/docs/sql-reference/statements/select/group-by#cube-modifier) are modifiers to GROUP BY. Both of these calculate subtotals. ROLLUP takes an ordered list of columns, for example `(day, month, year)`, and calculates subtotals at each level of the aggregation and then a grand total. CUBE calculates subtotals across all possible combinations of the columns specified. GROUPING identifies which rows returned by ROLLUP or CUBE are superaggregates, and which are rows that would be returned by an unmodified GROUP BY. The GROUPING function takes multiple columns as an argument, and returns a bitmask. - `1` indicates that a row returned by a `ROLLUP` or `CUBE` modifier to `GROUP BY` is a subtotal - `0` indicates that a row returned by a `ROLLUP` or `CUBE` is a row that is not a subtotal ## GROUPING SETS[​](https://clickhouse.com/docs/sql-reference/aggregate-functions/grouping_function#grouping-sets "Direct link to GROUPING SETS") By default, the CUBE modifier calculates subtotals for all possible combinations of the columns passed to CUBE. GROUPING SETS allows you to specify the specific combinations to calculate. Analyzing hierarchical data is a good use case for ROLLUP, CUBE, and GROUPING SETS modifiers. The sample here is a table containing data about what Linux distribution, and the version of that distribution is installed across two datacenters. It may be valuable to look at the data by distribution, version, and location. ### Load sample data[​](https://clickhouse.com/docs/sql-reference/aggregate-functions/grouping_function#load-sample-data "Direct link to Load sample data") ``` CREATE TABLE servers ( datacenter VARCHAR(255), distro VARCHAR(255) NOT NULL, version VARCHAR(50) NOT NULL, quantity INT ) ORDER BY (datacenter, distro, version) ``` ``` INSERT INTO servers(datacenter, distro, version, quantity) VALUES ('Schenectady', 'Arch','2022.08.05',50), ('Westport', 'Arch','2022.08.05',40), ('Schenectady','Arch','2021.09.01',30), ('Westport', 'Arch','2021.09.01',20), ('Schenectady','Arch','2020.05.01',10), ('Westport', 'Arch','2020.05.01',5), ('Schenectady','RHEL','9',60), ('Westport','RHEL','9',70), ('Westport','RHEL','7',80), ('Schenectady','RHEL','7',80) ``` ``` SELECT * FROM servers; ``` ``` β”Œβ”€datacenter──┬─distro─┬─version────┬─quantity─┐ β”‚ Schenectady β”‚ Arch β”‚ 2020.05.01 β”‚ 10 β”‚ β”‚ Schenectady β”‚ Arch β”‚ 2021.09.01 β”‚ 30 β”‚ β”‚ Schenectady β”‚ Arch β”‚ 2022.08.05 β”‚ 50 β”‚ β”‚ Schenectady β”‚ RHEL β”‚ 7 β”‚ 80 β”‚ β”‚ Schenectady β”‚ RHEL β”‚ 9 β”‚ 60 β”‚ β”‚ Westport β”‚ Arch β”‚ 2020.05.01 β”‚ 5 β”‚ β”‚ Westport β”‚ Arch β”‚ 2021.09.01 β”‚ 20 β”‚ β”‚ Westport β”‚ Arch β”‚ 2022.08.05 β”‚ 40 β”‚ β”‚ Westport β”‚ RHEL β”‚ 7 β”‚ 80 β”‚ β”‚ Westport β”‚ RHEL β”‚ 9 β”‚ 70 β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ 10 rows in set. Elapsed: 0.409 sec. ``` ### Simple queries[​](https://clickhouse.com/docs/sql-reference/aggregate-functions/grouping_function#simple-queries "Direct link to Simple queries") Get the count of servers in each data center by distribution: ``` SELECT datacenter, distro, SUM (quantity) qty FROM servers GROUP BY datacenter, distro; ``` ``` β”Œβ”€datacenter──┬─distro─┬─qty─┐ β”‚ Schenectady β”‚ RHEL β”‚ 140 β”‚ β”‚ Westport β”‚ Arch β”‚ 65 β”‚ β”‚ Schenectady β”‚ Arch β”‚ 90 β”‚ β”‚ Westport β”‚ RHEL β”‚ 150 β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”˜ 4 rows in set. Elapsed: 0.212 sec. ``` ``` SELECT datacenter, SUM (quantity) qty FROM servers GROUP BY datacenter; ``` ``` β”Œβ”€datacenter──┬─qty─┐ β”‚ Westport β”‚ 215 β”‚ β”‚ Schenectady β”‚ 230 β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”˜ 2 rows in set. Elapsed: 0.277 sec. ``` ``` SELECT distro, SUM (quantity) qty FROM servers GROUP BY distro; ``` ``` β”Œβ”€distro─┬─qty─┐ β”‚ Arch β”‚ 155 β”‚ β”‚ RHEL β”‚ 290 β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”˜ 2 rows in set. Elapsed: 0.352 sec. ``` ``` SELECT SUM(quantity) qty FROM servers; ``` ``` β”Œβ”€qty─┐ β”‚ 445 β”‚ β””β”€β”€β”€β”€β”€β”˜ 1 row in set. Elapsed: 0.244 sec. ``` ### Comparing multiple GROUP BY statements with GROUPING SETS[​](https://clickhouse.com/docs/sql-reference/aggregate-functions/grouping_function#comparing-multiple-group-by-statements-with-grouping-sets "Direct link to Comparing multiple GROUP BY statements with GROUPING SETS") Breaking down the data without CUBE, ROLLUP, or GROUPING SETS: ``` SELECT datacenter, distro, SUM (quantity) qty FROM servers GROUP BY datacenter, distro UNION ALL SELECT datacenter, null, SUM (quantity) qty FROM servers GROUP BY datacenter UNION ALL SELECT null, distro, SUM (quantity) qty FROM servers GROUP BY distro UNION ALL SELECT null, null, SUM(quantity) qty FROM servers; ``` ``` β”Œβ”€datacenter─┬─distro─┬─qty─┐ β”‚ ᴺᡁᴸᴸ β”‚ ᴺᡁᴸᴸ β”‚ 445 β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”˜ β”Œβ”€datacenter──┬─distro─┬─qty─┐ β”‚ Westport β”‚ ᴺᡁᴸᴸ β”‚ 215 β”‚ β”‚ Schenectady β”‚ ᴺᡁᴸᴸ β”‚ 230 β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”˜ β”Œβ”€datacenter──┬─distro─┬─qty─┐ β”‚ Schenectady β”‚ RHEL β”‚ 140 β”‚ β”‚ Westport β”‚ Arch β”‚ 65 β”‚ β”‚ Schenectady β”‚ Arch β”‚ 90 β”‚ β”‚ Westport β”‚ RHEL β”‚ 150 β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”˜ β”Œβ”€datacenter─┬─distro─┬─qty─┐ β”‚ ᴺᡁᴸᴸ β”‚ Arch β”‚ 155 β”‚ β”‚ ᴺᡁᴸᴸ β”‚ RHEL β”‚ 290 β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”˜ 9 rows in set. Elapsed: 0.527 sec. ``` Getting the same information using GROUPING SETS: ``` SELECT datacenter, distro, SUM (quantity) qty FROM servers GROUP BY GROUPING SETS( (datacenter,distro), (datacenter), (distro), () ) ``` ``` β”Œβ”€datacenter──┬─distro─┬─qty─┐ β”‚ Schenectady β”‚ RHEL β”‚ 140 β”‚ β”‚ Westport β”‚ Arch β”‚ 65 β”‚ β”‚ Schenectady β”‚ Arch β”‚ 90 β”‚ β”‚ Westport β”‚ RHEL β”‚ 150 β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”˜ β”Œβ”€datacenter──┬─distro─┬─qty─┐ β”‚ Westport β”‚ β”‚ 215 β”‚ β”‚ Schenectady β”‚ β”‚ 230 β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”˜ β”Œβ”€datacenter─┬─distro─┬─qty─┐ β”‚ β”‚ β”‚ 445 β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”˜ β”Œβ”€datacenter─┬─distro─┬─qty─┐ β”‚ β”‚ Arch β”‚ 155 β”‚ β”‚ β”‚ RHEL β”‚ 290 β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”˜ 9 rows in set. Elapsed: 0.427 sec. ``` ### Comparing CUBE with GROUPING SETS[​](https://clickhouse.com/docs/sql-reference/aggregate-functions/grouping_function#comparing-cube-with-grouping-sets "Direct link to Comparing CUBE with GROUPING SETS") The CUBE in the next query, `CUBE(datacenter,distro,version)` provides a hierarchy that may not make sense. It does not make sense to look at Version across the two distributions (as Arch and RHEL do not have the same release cycle or version naming standards). The GROUPING SETS example following this one is more appropriate as it groups `distro` and `version` in the same set. ``` SELECT datacenter, distro, version, SUM(quantity) FROM servers GROUP BY CUBE(datacenter,distro,version) ORDER BY datacenter, distro; ``` ``` β”Œβ”€datacenter──┬─distro─┬─version────┬─sum(quantity)─┐ β”‚ β”‚ β”‚ 7 β”‚ 160 β”‚ β”‚ β”‚ β”‚ 2020.05.01 β”‚ 15 β”‚ β”‚ β”‚ β”‚ 2021.09.01 β”‚ 50 β”‚ β”‚ β”‚ β”‚ 2022.08.05 β”‚ 90 β”‚ β”‚ β”‚ β”‚ 9 β”‚ 130 β”‚ β”‚ β”‚ β”‚ β”‚ 445 β”‚ β”‚ β”‚ Arch β”‚ 2021.09.01 β”‚ 50 β”‚ β”‚ β”‚ Arch β”‚ 2022.08.05 β”‚ 90 β”‚ β”‚ β”‚ Arch β”‚ 2020.05.01 β”‚ 15 β”‚ β”‚ β”‚ Arch β”‚ β”‚ 155 β”‚ β”‚ β”‚ RHEL β”‚ 9 β”‚ 130 β”‚ β”‚ β”‚ RHEL β”‚ 7 β”‚ 160 β”‚ β”‚ β”‚ RHEL β”‚ β”‚ 290 β”‚ β”‚ Schenectady β”‚ β”‚ 9 β”‚ 60 β”‚ β”‚ Schenectady β”‚ β”‚ 2021.09.01 β”‚ 30 β”‚ β”‚ Schenectady β”‚ β”‚ 7 β”‚ 80 β”‚ β”‚ Schenectady β”‚ β”‚ 2022.08.05 β”‚ 50 β”‚ β”‚ Schenectady β”‚ β”‚ 2020.05.01 β”‚ 10 β”‚ β”‚ Schenectady β”‚ β”‚ β”‚ 230 β”‚ β”‚ Schenectady β”‚ Arch β”‚ 2022.08.05 β”‚ 50 β”‚ β”‚ Schenectady β”‚ Arch β”‚ 2021.09.01 β”‚ 30 β”‚ β”‚ Schenectady β”‚ Arch β”‚ 2020.05.01 β”‚ 10 β”‚ β”‚ Schenectady β”‚ Arch β”‚ β”‚ 90 β”‚ β”‚ Schenectady β”‚ RHEL β”‚ 7 β”‚ 80 β”‚ β”‚ Schenectady β”‚ RHEL β”‚ 9 β”‚ 60 β”‚ β”‚ Schenectady β”‚ RHEL β”‚ β”‚ 140 β”‚ β”‚ Westport β”‚ β”‚ 9 β”‚ 70 β”‚ β”‚ Westport β”‚ β”‚ 2020.05.01 β”‚ 5 β”‚ β”‚ Westport β”‚ β”‚ 2022.08.05 β”‚ 40 β”‚ β”‚ Westport β”‚ β”‚ 7 β”‚ 80 β”‚ β”‚ Westport β”‚ β”‚ 2021.09.01 β”‚ 20 β”‚ β”‚ Westport β”‚ β”‚ β”‚ 215 β”‚ β”‚ Westport β”‚ Arch β”‚ 2020.05.01 β”‚ 5 β”‚ β”‚ Westport β”‚ Arch β”‚ 2021.09.01 β”‚ 20 β”‚ β”‚ Westport β”‚ Arch β”‚ 2022.08.05 β”‚ 40 β”‚ β”‚ Westport β”‚ Arch β”‚ β”‚ 65 β”‚ β”‚ Westport β”‚ RHEL β”‚ 9 β”‚ 70 β”‚ β”‚ Westport β”‚ RHEL β”‚ 7 β”‚ 80 β”‚ β”‚ Westport β”‚ RHEL β”‚ β”‚ 150 β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ 39 rows in set. Elapsed: 0.355 sec. ``` Note Version in the above example may not make sense when it is not associated with a distro, if we were tracking the kernel version it might make sense because the kernel version can be associated with either distro. Using GROUPING SETS, as in the next example, may be a better choice. ``` SELECT datacenter, distro, version, SUM(quantity) FROM servers GROUP BY GROUPING SETS ( (datacenter, distro, version), (datacenter, distro)) ``` ``` β”Œβ”€datacenter──┬─distro─┬─version────┬─sum(quantity)─┐ β”‚ Westport β”‚ RHEL β”‚ 9 β”‚ 70 β”‚ β”‚ Schenectady β”‚ Arch β”‚ 2022.08.05 β”‚ 50 β”‚ β”‚ Schenectady β”‚ Arch β”‚ 2021.09.01 β”‚ 30 β”‚ β”‚ Schenectady β”‚ RHEL β”‚ 7 β”‚ 80 β”‚ β”‚ Westport β”‚ Arch β”‚ 2020.05.01 β”‚ 5 β”‚ β”‚ Westport β”‚ RHEL β”‚ 7 β”‚ 80 β”‚ β”‚ Westport β”‚ Arch β”‚ 2021.09.01 β”‚ 20 β”‚ β”‚ Westport β”‚ Arch β”‚ 2022.08.05 β”‚ 40 β”‚ β”‚ Schenectady β”‚ RHEL β”‚ 9 β”‚ 60 β”‚ β”‚ Schenectady β”‚ Arch β”‚ 2020.05.01 β”‚ 10 β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”Œβ”€datacenter──┬─distro─┬─version─┬─sum(quantity)─┐ β”‚ Schenectady β”‚ RHEL β”‚ β”‚ 140 β”‚ β”‚ Westport β”‚ Arch β”‚ β”‚ 65 β”‚ β”‚ Schenectady β”‚ Arch β”‚ β”‚ 90 β”‚ β”‚ Westport β”‚ RHEL β”‚ β”‚ 150 β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ 14 rows in set. Elapsed: 1.036 sec. ```
Shard89 (laksa)
Root Hash12633450985039531489
Unparsed URLcom,clickhouse!/docs/sql-reference/aggregate-functions/grouping_function s443