βΉοΈ Skipped - page is already crawled
| Filter | Status | Condition | Details |
|---|---|---|---|
| HTTP status | PASS | download_http_code = 200 | HTTP 200 |
| Age cutoff | PASS | download_stamp > now() - 6 MONTH | 0.1 months ago |
| History drop | PASS | isNull(history_drop_reason) | No drop reason |
| Spam/ban | PASS | fh_dont_index != 1 AND ml_spam_score = 0 | ml_spam_score=0 |
| Canonical | PASS | meta_canonical IS NULL OR = '' OR = src_unparsed | Not set |
| Property | Value |
|---|---|
| URL | https://clickhouse.com/docs/sql-reference/aggregate-functions/grouping_function |
| Last Crawled | 2026-04-12 07:34:47 (2 days ago) |
| First Indexed | 2025-02-21 02:43:43 (1 year ago) |
| HTTP Status Code | 200 |
| Meta Title | GROUPING | ClickHouse Docs |
| Meta Description | Documentation for the GROUPING aggregate function. |
| Meta Canonical | null |
| 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)
[](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)
[](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)

Β© 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://clickhouse.com/)
EN
- Get startedβΌ
- CloudβΌ
- Manage dataβΌ
- Server adminβΌ
- ReferenceβΌ
- IntegrationsβΌ
- ClickStackβΌ
- chDBβΌ
- AboutβΌ
[](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.
``` |
| Shard | 89 (laksa) |
| Root Hash | 12633450985039531489 |
| Unparsed URL | com,clickhouse!/docs/sql-reference/aggregate-functions/grouping_function s443 |