βΉοΈ 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 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 |
| Last Crawled | 2026-04-11 03:09:40 (11 hours ago) |
| First Indexed | 2025-02-21 00:33:21 (1 year ago) |
| HTTP Status Code | 200 |
| Meta Title | Aggregate Functions | ClickHouse Docs |
| Meta Description | Documentation for Aggregate Functions |
| Meta Canonical | null |
| Boilerpipe Text | Aggregate functions work in the
normal
way as expected by database experts.
ClickHouse also supports:
Parametric aggregate functions
, which accept other parameters in addition to columns.
Combinators
, which change the behavior of aggregate functions.
NULL processing
β
During aggregation, all
NULL
arguments are skipped. If the aggregation has several arguments it will ignore any row in which one or more of them are NULL.
There is an exception to this rule, which are the functions
first_value
,
last_value
and their aliases (
any
and
anyLast
respectively) when followed by the modifier
RESPECT NULLS
. For example,
FIRST_VALUE(b) RESPECT NULLS
.
Examples:
Consider this table:
ββxββ¬ββββyββ
β 1 β 2 β
β 2 β α΄Ία΅α΄Έα΄Έ β
β 3 β 2 β
β 3 β 3 β
β 3 β α΄Ία΅α΄Έα΄Έ β
βββββ΄βββββββ
Let's say you need to total the values in the
y
column:
SELECT
sum
(
y
)
FROM
t_null_big
ββsum(y)ββ
β 7 β
ββββββββββ
Now you can use the
groupArray
function to create an array from the
y
column:
SELECT
groupArray
(
y
)
FROM
t_null_big
ββgroupArray(y)ββ
β [2,2,3] β
βββββββββββββββββ
groupArray
does not include
NULL
in the resulting array.
You can use
COALESCE
to change NULL into a value that makes sense in your use case. For example:
avg(COALESCE(column, 0))
with use the column value in the aggregation or zero if NULL:
SELECT
avg
(
y
)
,
avg
(
coalesce
(
y
,
0
)
)
FROM
t_null_big
ββββββββββββββavg(y)ββ¬βavg(coalesce(y, 0))ββ
β 2.3333333333333335 β 1.4 β
ββββββββββββββββββββββ΄ββββββββββββββββββββββ
Also you can use
Tuple
to work around NULL skipping behavior. A
Tuple
that contains only a
NULL
value is not
NULL
, so the aggregate functions won't skip that row because of that
NULL
value.
SELECT
groupArray
(
y
)
,
groupArray
(
tuple
(
y
)
)
.1
FROM
t_null_big
;
ββgroupArray
(
y
)
ββ¬βtupleElement
(
groupArray
(
tuple
(
y
)
)
,
1
)
ββ
β
[
2
,
2
,
3
]
β
[
2
,
NULL
,
2
,
3
,
NULL
]
β
βββββββββββββββββ΄ββββββββββββββββββββββββββββββββββββββββ
Note that aggregations are skipped when the columns are used as arguments to an aggregated function. For example
count
without parameters (
count()
) or with constant ones (
count(1)
) will count all rows in the block (independently of the value of the GROUP BY column as it's not an argument), while
count(column)
will only return the number of rows where column is not NULL.
SELECT
v
,
count
(
1
)
,
count
(
v
)
FROM
(
SELECT
if
(
number
<
10
,
NULL
,
number
%
3
)
AS
v
FROM
numbers
(
15
)
)
GROUP
BY
v
βββββvββ¬β
count
(
)
ββ¬β
count
(
v
)
ββ
β α΄Ία΅α΄Έα΄Έ β
10
β
0
β
β
0
β
1
β
1
β
β
1
β
2
β
2
β
β
2
β
2
β
2
β
ββββββββ΄ββββββββββ΄βββββββββββ
And here is an example of first_value with
RESPECT NULLS
where we can see that NULL inputs are respected and it will return the first value read, whether it's NULL or not:
SELECT
col
||
'_'
||
(
(
col
+
1
)
*
5
-
1
)
AS
range
,
first_value
(
odd_or_null
)
AS
first
,
first_value
(
odd_or_null
)
IGNORE
NULLS
as
first_ignore_null
,
first_value
(
odd_or_null
)
RESPECT NULLS
as
first_respect_nulls
FROM
(
SELECT
intDiv
(
number
,
5
)
AS
col
,
if
(
number
%
2
=
=
0
,
NULL
,
number
)
AS
odd_or_null
FROM
numbers
(
15
)
)
GROUP
BY
col
ORDER
BY
col
ββrangeββ¬β
first
ββ¬βfirst_ignore_nullββ¬βfirst_respect_nullsββ
β
0
_4 β
1
β
1
β α΄Ία΅α΄Έα΄Έ β
β
1
_9 β
5
β
5
β
5
β
β
2
_14 β
11
β
11
β α΄Ία΅α΄Έα΄Έ β
βββββββββ΄ββββββββ΄ββββββββββββββββββββ΄ββββββββββββββββββββββ |
| Markdown | [Skip to main content](https://clickhouse.com/docs/sql-reference/aggregate-functions#__docusaurus_skipToContent_fallback)
[](https://clickhouse.com/)
- [Products](https://clickhouse.com/docs/sql-reference/aggregate-functions)
- [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)
- [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)
- [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)
- [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=19a5afde-4d87-4da3-ba89-ef87a430ca37&pagePath=%2Fdocs%2Fsql-reference%2Faggregate-functions&origPath=%2Fdocs%2Fsql-reference%2Faggregate-functions&utm_ga=GA1.1.1113250799.1775876986)
[Get started](https://console.clickhouse.cloud/signUp?loc=docs-nav-signUp-cta&glxid=19a5afde-4d87-4da3-ba89-ef87a430ca37&pagePath=%2Fdocs%2Fsql-reference%2Faggregate-functions&origPath=%2Fdocs%2Fsql-reference%2Faggregate-functions&utm_ga=GA1.1.1113250799.1775876986)
[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)
- [English](https://clickhouse.com/docs/sql-reference/aggregate-functions)
- [ζ₯ζ¬θͺ](https://clickhouse.com/docs/jp/sql-reference/aggregate-functions)
- [δΈζ](https://clickhouse.com/docs/zh/sql-reference/aggregate-functions)
- [Π ΡΡΡΠΊΠΈΠΉ](https://clickhouse.com/docs/ru/sql-reference/aggregate-functions)
- [νκ΅μ΄](https://clickhouse.com/docs/ko/sql-reference/aggregate-functions)
[Skip to main content](https://clickhouse.com/docs/sql-reference/aggregate-functions#__docusaurus_skipToContent_fallback)
[](https://clickhouse.com/)
- [Products](https://clickhouse.com/docs/sql-reference/aggregate-functions)
- [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)
- [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)
- [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)
- [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=19a5afde-4d87-4da3-ba89-ef87a430ca37&pagePath=%2Fdocs%2Fsql-reference%2Faggregate-functions&origPath=%2Fdocs%2Fsql-reference%2Faggregate-functions&utm_ga=GA1.1.1113250799.1775876986)
[Get started](https://console.clickhouse.cloud/signUp?loc=docs-nav-signUp-cta&glxid=19a5afde-4d87-4da3-ba89-ef87a430ca37&pagePath=%2Fdocs%2Fsql-reference%2Faggregate-functions&origPath=%2Fdocs%2Fsql-reference%2Faggregate-functions&utm_ga=GA1.1.1113250799.1775876986)
[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)
- [English](https://clickhouse.com/docs/sql-reference/aggregate-functions)
- [ζ₯ζ¬θͺ](https://clickhouse.com/docs/jp/sql-reference/aggregate-functions)
- [δΈζ](https://clickhouse.com/docs/zh/sql-reference/aggregate-functions)
- [Π ΡΡΡΠΊΠΈΠΉ](https://clickhouse.com/docs/ru/sql-reference/aggregate-functions)
- [νκ΅μ΄](https://clickhouse.com/docs/ko/sql-reference/aggregate-functions)
[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)
- [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
[Edit this page](https://github.com/ClickHouse/ClickHouse/tree/master/docs/en/sql-reference/aggregate-functions/index.md)
# Aggregate functions
Aggregate functions work in the [normal](http://www.sql-tutorial.com/sql-aggregate-functions-sql-tutorial) way as expected by database experts.
ClickHouse also supports:
- [Parametric aggregate functions](https://clickhouse.com/docs/sql-reference/aggregate-functions/parametric-functions), which accept other parameters in addition to columns.
- [Combinators](https://clickhouse.com/docs/sql-reference/aggregate-functions/combinators), which change the behavior of aggregate functions.
## NULL processing[β](https://clickhouse.com/docs/sql-reference/aggregate-functions#null-processing "Direct link to NULL processing")
During aggregation, all `NULL` arguments are skipped. If the aggregation has several arguments it will ignore any row in which one or more of them are NULL.
There is an exception to this rule, which are the functions [`first_value`](https://clickhouse.com/docs/sql-reference/aggregate-functions/reference/first_value), [`last_value`](https://clickhouse.com/docs/sql-reference/aggregate-functions/reference/last_value) and their aliases (`any` and `anyLast` respectively) when followed by the modifier `RESPECT NULLS`. For example, `FIRST_VALUE(b) RESPECT NULLS`.
**Examples:**
Consider this table:
```
ββxββ¬ββββyββ
β 1 β 2 β
β 2 β α΄Ία΅α΄Έα΄Έ β
β 3 β 2 β
β 3 β 3 β
β 3 β α΄Ία΅α΄Έα΄Έ β
βββββ΄βββββββ
```
Let's say you need to total the values in the `y` column:
```
SELECT sum(y) FROM t_null_big
```
```
ββsum(y)ββ
β 7 β
ββββββββββ
```
Now you can use the `groupArray` function to create an array from the `y` column:
```
SELECT groupArray(y) FROM t_null_big
```
```
ββgroupArray(y)ββ
β [2,2,3] β
βββββββββββββββββ
```
`groupArray` does not include `NULL` in the resulting array.
You can use [COALESCE](https://clickhouse.com/docs/sql-reference/functions/functions-for-nulls#coalesce) to change NULL into a value that makes sense in your use case. For example: `avg(COALESCE(column, 0))` with use the column value in the aggregation or zero if NULL:
```
SELECT
avg(y),
avg(coalesce(y, 0))
FROM t_null_big
```
```
ββββββββββββββavg(y)ββ¬βavg(coalesce(y, 0))ββ
β 2.3333333333333335 β 1.4 β
ββββββββββββββββββββββ΄ββββββββββββββββββββββ
```
Also you can use [Tuple](https://clickhouse.com/docs/sql-reference/data-types/tuple) to work around NULL skipping behavior. A `Tuple` that contains only a `NULL` value is not `NULL`, so the aggregate functions won't skip that row because of that `NULL` value.
```
SELECT
groupArray(y),
groupArray(tuple(y)).1
FROM t_null_big;
ββgroupArray(y)ββ¬βtupleElement(groupArray(tuple(y)), 1)ββ
β [2,2,3] β [2,NULL,2,3,NULL] β
βββββββββββββββββ΄ββββββββββββββββββββββββββββββββββββββββ
```
Note that aggregations are skipped when the columns are used as arguments to an aggregated function. For example [`count`](https://clickhouse.com/docs/sql-reference/aggregate-functions/reference/count) without parameters (`count()`) or with constant ones (`count(1)`) will count all rows in the block (independently of the value of the GROUP BY column as it's not an argument), while `count(column)` will only return the number of rows where column is not NULL.
```
SELECT
v,
count(1),
count(v)
FROM
(
SELECT if(number < 10, NULL, number % 3) AS v
FROM numbers(15)
)
GROUP BY v
βββββvββ¬βcount()ββ¬βcount(v)ββ
β α΄Ία΅α΄Έα΄Έ β 10 β 0 β
β 0 β 1 β 1 β
β 1 β 2 β 2 β
β 2 β 2 β 2 β
ββββββββ΄ββββββββββ΄βββββββββββ
```
And here is an example of first\_value with `RESPECT NULLS` where we can see that NULL inputs are respected and it will return the first value read, whether it's NULL or not:
```
SELECT
col || '_' || ((col + 1) * 5 - 1) AS range,
first_value(odd_or_null) AS first,
first_value(odd_or_null) IGNORE NULLS as first_ignore_null,
first_value(odd_or_null) RESPECT NULLS as first_respect_nulls
FROM
(
SELECT
intDiv(number, 5) AS col,
if(number % 2 == 0, NULL, number) AS odd_or_null
FROM numbers(15)
)
GROUP BY col
ORDER BY col
ββrangeββ¬βfirstββ¬βfirst_ignore_nullββ¬βfirst_respect_nullsββ
β 0_4 β 1 β 1 β α΄Ία΅α΄Έα΄Έ β
β 1_9 β 5 β 5 β 5 β
β 2_14 β 11 β 11 β α΄Ία΅α΄Έα΄Έ β
βββββββββ΄ββββββββ΄ββββββββββββββββββββ΄ββββββββββββββββββββββ
```
[Previous WebAssembly UDFs](https://clickhouse.com/docs/sql-reference/functions/wasm_udf)
[Next Aggregate Functions](https://clickhouse.com/docs/sql-reference/aggregate-functions/reference)
- [NULL processing](https://clickhouse.com/docs/sql-reference/aggregate-functions#null-processing)
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=19a5afde-4d87-4da3-ba89-ef87a430ca37&pagePath=%2Fdocs%2Fsql-reference%2Faggregate-functions&origPath=%2Fdocs%2Fsql-reference%2Faggregate-functions&utm_ga=GA1.1.1113250799.1775876986)
Β© 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βΌ
- FormatsβΌ
- [Data Lakes](https://clickhouse.com/docs/sql-reference/datalakes) |
| Readable Markdown | Aggregate functions work in the [normal](http://www.sql-tutorial.com/sql-aggregate-functions-sql-tutorial) way as expected by database experts.
ClickHouse also supports:
- [Parametric aggregate functions](https://clickhouse.com/docs/sql-reference/aggregate-functions/parametric-functions), which accept other parameters in addition to columns.
- [Combinators](https://clickhouse.com/docs/sql-reference/aggregate-functions/combinators), which change the behavior of aggregate functions.
## NULL processing[β](https://clickhouse.com/docs/sql-reference/aggregate-functions#null-processing "Direct link to NULL processing")
During aggregation, all `NULL` arguments are skipped. If the aggregation has several arguments it will ignore any row in which one or more of them are NULL.
There is an exception to this rule, which are the functions [`first_value`](https://clickhouse.com/docs/sql-reference/aggregate-functions/reference/first_value), [`last_value`](https://clickhouse.com/docs/sql-reference/aggregate-functions/reference/last_value) and their aliases (`any` and `anyLast` respectively) when followed by the modifier `RESPECT NULLS`. For example, `FIRST_VALUE(b) RESPECT NULLS`.
**Examples:**
Consider this table:
```
ββxββ¬ββββyββ
β 1 β 2 β
β 2 β α΄Ία΅α΄Έα΄Έ β
β 3 β 2 β
β 3 β 3 β
β 3 β α΄Ία΅α΄Έα΄Έ β
βββββ΄βββββββ
```
Let's say you need to total the values in the `y` column:
```
SELECT sum(y) FROM t_null_big
```
```
ββsum(y)ββ
β 7 β
ββββββββββ
```
Now you can use the `groupArray` function to create an array from the `y` column:
```
SELECT groupArray(y) FROM t_null_big
```
```
ββgroupArray(y)ββ
β [2,2,3] β
βββββββββββββββββ
```
`groupArray` does not include `NULL` in the resulting array.
You can use [COALESCE](https://clickhouse.com/docs/sql-reference/functions/functions-for-nulls#coalesce) to change NULL into a value that makes sense in your use case. For example: `avg(COALESCE(column, 0))` with use the column value in the aggregation or zero if NULL:
```
SELECT
avg(y),
avg(coalesce(y, 0))
FROM t_null_big
```
```
ββββββββββββββavg(y)ββ¬βavg(coalesce(y, 0))ββ
β 2.3333333333333335 β 1.4 β
ββββββββββββββββββββββ΄ββββββββββββββββββββββ
```
Also you can use [Tuple](https://clickhouse.com/docs/sql-reference/data-types/tuple) to work around NULL skipping behavior. A `Tuple` that contains only a `NULL` value is not `NULL`, so the aggregate functions won't skip that row because of that `NULL` value.
```
SELECT
groupArray(y),
groupArray(tuple(y)).1
FROM t_null_big;
ββgroupArray(y)ββ¬βtupleElement(groupArray(tuple(y)), 1)ββ
β [2,2,3] β [2,NULL,2,3,NULL] β
βββββββββββββββββ΄ββββββββββββββββββββββββββββββββββββββββ
```
Note that aggregations are skipped when the columns are used as arguments to an aggregated function. For example [`count`](https://clickhouse.com/docs/sql-reference/aggregate-functions/reference/count) without parameters (`count()`) or with constant ones (`count(1)`) will count all rows in the block (independently of the value of the GROUP BY column as it's not an argument), while `count(column)` will only return the number of rows where column is not NULL.
```
SELECT
v,
count(1),
count(v)
FROM
(
SELECT if(number < 10, NULL, number % 3) AS v
FROM numbers(15)
)
GROUP BY v
βββββvββ¬βcount()ββ¬βcount(v)ββ
β α΄Ία΅α΄Έα΄Έ β 10 β 0 β
β 0 β 1 β 1 β
β 1 β 2 β 2 β
β 2 β 2 β 2 β
ββββββββ΄ββββββββββ΄βββββββββββ
```
And here is an example of first\_value with `RESPECT NULLS` where we can see that NULL inputs are respected and it will return the first value read, whether it's NULL or not:
```
SELECT
col || '_' || ((col + 1) * 5 - 1) AS range,
first_value(odd_or_null) AS first,
first_value(odd_or_null) IGNORE NULLS as first_ignore_null,
first_value(odd_or_null) RESPECT NULLS as first_respect_nulls
FROM
(
SELECT
intDiv(number, 5) AS col,
if(number % 2 == 0, NULL, number) AS odd_or_null
FROM numbers(15)
)
GROUP BY col
ORDER BY col
ββrangeββ¬βfirstββ¬βfirst_ignore_nullββ¬βfirst_respect_nullsββ
β 0_4 β 1 β 1 β α΄Ία΅α΄Έα΄Έ β
β 1_9 β 5 β 5 β 5 β
β 2_14 β 11 β 11 β α΄Ία΅α΄Έα΄Έ β
βββββββββ΄ββββββββ΄ββββββββββββββββββββ΄ββββββββββββββββββββββ
``` |
| Shard | 89 (laksa) |
| Root Hash | 12633450985039531489 |
| Unparsed URL | com,clickhouse!/docs/sql-reference/aggregate-functions s443 |