πŸ•·οΈ Crawler Inspector

URL Lookup

Direct Parameter Lookup

Raw Queries and Responses

1. Shard Calculation

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

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
11 hours ago
πŸ€–
ROBOTS ALLOWED

Page Info Filters

FilterStatusConditionDetails
HTTP statusPASSdownload_http_code = 200HTTP 200
Age cutoffPASSdownload_stamp > now() - 6 MONTH0 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
Last Crawled2026-04-11 03:09:40 (11 hours ago)
First Indexed2025-02-21 00:33:21 (1 year ago)
HTTP Status Code200
Meta TitleAggregate Functions | ClickHouse Docs
Meta DescriptionDocumentation for Aggregate Functions
Meta Canonicalnull
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) [![ClickHouse](https://clickhouse.com/docs/img/ch_logo_docs.svg)](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) [![ClickHouse](https://clickhouse.com/docs/img/ch_logo_docs.svg)](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) ![](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β–Ό - 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 β”‚ ᴺᡁᴸᴸ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ ```
Shard89 (laksa)
Root Hash12633450985039531489
Unparsed URLcom,clickhouse!/docs/sql-reference/aggregate-functions s443