🕷️ Crawler Inspector

URL Lookup

Direct Parameter Lookup

Raw Queries and Responses

1. Shard Calculation

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

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
10 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/data-types/aggregatefunction
Last Crawled2026-04-17 04:36:17 (10 hours ago)
First Indexed2025-02-21 00:54:29 (1 year ago)
HTTP Status Code200
Meta TitleAggregateFunction Type | ClickHouse Docs
Meta DescriptionDocumentation for the AggregateFunction data type in ClickHouse, which stores intermediate states of aggregate functions
Meta Canonicalnull
Boilerpipe Text
Description ​ All Aggregate functions in ClickHouse have an implementation-specific intermediate state that can be serialized to an AggregateFunction data type and stored in a table. This is usually done by means of a materialized view . There are two aggregate function combinators commonly used with the AggregateFunction type: The -State aggregate function combinator, which when appended to an aggregate function name, produces AggregateFunction intermediate states. The -Merge aggregate function combinator, which is used to get the final result of an aggregation from the intermediate states. Syntax ​ AggregateFunction ( aggregate_function_name , types_of_arguments . . . ) Parameters aggregate_function_name - The name of an aggregate function. If the function is parametric, then its parameters should be specified too. types_of_arguments - The types of the aggregate function arguments. for example: CREATE TABLE t ( column1 AggregateFunction ( uniq , UInt64 ) , column2 AggregateFunction ( anyIf , String , UInt8 ) , column3 AggregateFunction ( quantiles ( 0.5 , 0.9 ) , UInt64 ) ) ENGINE = . . . Usage ​ Data Insertion ​ To insert data into a table with columns of type AggregateFunction , you can use INSERT SELECT with aggregate functions and the -State aggregate function combinator. For example, to insert into columns of type AggregateFunction(uniq, UInt64) and AggregateFunction(quantiles(0.5, 0.9), UInt64) you would use the following aggregate functions with combinators. uniqState ( UserID ) quantilesState ( 0.5 , 0.9 ) ( SendTiming ) In contrast to functions uniq and quantiles , uniqState and quantilesState (with -State combinator appended) return the state, rather than the final value. In other words, they return a value of AggregateFunction type. In the results of the SELECT query, values of type AggregateFunction have implementation-specific binary representations for all of the ClickHouse output formats. There is a special Session level setting aggregate_function_input_format that allows to build state from the input values. It supports the following formats: state - binary string with the serialized state (the default). If you dump data into, for example, the TabSeparated format with a SELECT query, then this dump can be loaded back using the INSERT query. value - the format will expect a single value of the argument of the aggregate function, or in the case of multiple arguments, a tuple of them; that will be deserialized to form the relevant state array - the format will expect an Array of values, as described in the values option above; all the elements of the array will be aggregated to form the state Data Selection ​ When selecting data from AggregatingMergeTree table, use the GROUP BY clause and the same aggregate functions as for when you inserted the data, but use the -Merge combinator. An aggregate function with the -Merge combinator appended to it takes a set of states, combines them, and returns the result of the complete data aggregation. For example, the following two queries return the same result: SELECT uniq ( UserID ) FROM table SELECT uniqMerge ( state ) FROM ( SELECT uniqState ( UserID ) AS state FROM table GROUP BY RegionID ) Usage Example ​ See AggregatingMergeTree engine description. Related Content ​ Blog: Using Aggregate Combinators in ClickHouse MergeState combinator. State combinator.
Markdown
[Skip to main content](https://clickhouse.com/docs/sql-reference/data-types/aggregatefunction#__docusaurus_skipToContent_fallback) [![ClickHouse](https://clickhouse.com/docs/img/ch_logo_docs.svg)](https://clickhouse.com/) - [Products](https://clickhouse.com/docs/sql-reference/data-types/aggregatefunction) - [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/data-types/aggregatefunction) - [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/data-types/aggregatefunction) - [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/data-types/aggregatefunction) - [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\.9k](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=a0d5e403-d905-4068-8fc0-e2aae6645c9b&pagePath=%2Fdocs%2Fsql-reference%2Fdata-types%2Faggregatefunction&origPath=%2Fdocs%2Fsql-reference%2Fdata-types%2Faggregatefunction&utm_ga=GA1.1.1031461531.1776400579) [Get started](https://console.clickhouse.cloud/signUp?loc=docs-nav-signUp-cta&glxid=a0d5e403-d905-4068-8fc0-e2aae6645c9b&pagePath=%2Fdocs%2Fsql-reference%2Fdata-types%2Faggregatefunction&origPath=%2Fdocs%2Fsql-reference%2Fdata-types%2Faggregatefunction&utm_ga=GA1.1.1031461531.1776400579) [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/data-types/aggregatefunction) - [English](https://clickhouse.com/docs/sql-reference/data-types/aggregatefunction) - [日本語](https://clickhouse.com/docs/jp/sql-reference/data-types/aggregatefunction) - [中文](https://clickhouse.com/docs/zh/sql-reference/data-types/aggregatefunction) - [Русский](https://clickhouse.com/docs/ru/sql-reference/data-types/aggregatefunction) - [한국어](https://clickhouse.com/docs/ko/sql-reference/data-types/aggregatefunction) [Skip to main content](https://clickhouse.com/docs/sql-reference/data-types/aggregatefunction#__docusaurus_skipToContent_fallback) [![ClickHouse](https://clickhouse.com/docs/img/ch_logo_docs.svg)](https://clickhouse.com/) - [Products](https://clickhouse.com/docs/sql-reference/data-types/aggregatefunction) - [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/data-types/aggregatefunction) - [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/data-types/aggregatefunction) - [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/data-types/aggregatefunction) - [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\.9k](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=a0d5e403-d905-4068-8fc0-e2aae6645c9b&pagePath=%2Fdocs%2Fsql-reference%2Fdata-types%2Faggregatefunction&origPath=%2Fdocs%2Fsql-reference%2Fdata-types%2Faggregatefunction&utm_ga=GA1.1.1031461531.1776400579) [Get started](https://console.clickhouse.cloud/signUp?loc=docs-nav-signUp-cta&glxid=a0d5e403-d905-4068-8fc0-e2aae6645c9b&pagePath=%2Fdocs%2Fsql-reference%2Fdata-types%2Faggregatefunction&origPath=%2Fdocs%2Fsql-reference%2Fdata-types%2Faggregatefunction&utm_ga=GA1.1.1031461531.1776400579) [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/data-types/aggregatefunction) - [English](https://clickhouse.com/docs/sql-reference/data-types/aggregatefunction) - [日本語](https://clickhouse.com/docs/jp/sql-reference/data-types/aggregatefunction) - [中文](https://clickhouse.com/docs/zh/sql-reference/data-types/aggregatefunction) - [Русский](https://clickhouse.com/docs/ru/sql-reference/data-types/aggregatefunction) - [한국어](https://clickhouse.com/docs/ko/sql-reference/data-types/aggregatefunction) [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) - [Int \| UInt](https://clickhouse.com/docs/sql-reference/data-types/int-uint) - [Float32 \| Float64 \| BFloat16](https://clickhouse.com/docs/sql-reference/data-types/float) - [Decimal](https://clickhouse.com/docs/sql-reference/data-types/decimal) - [String](https://clickhouse.com/docs/sql-reference/data-types/string) - [FixedString(N)](https://clickhouse.com/docs/sql-reference/data-types/fixedstring) - [Date](https://clickhouse.com/docs/sql-reference/data-types/date) - [Date32](https://clickhouse.com/docs/sql-reference/data-types/date32) - [Time](https://clickhouse.com/docs/sql-reference/data-types/time) - [DateTime](https://clickhouse.com/docs/sql-reference/data-types/datetime) - [Time64](https://clickhouse.com/docs/sql-reference/data-types/time64) - [DateTime64](https://clickhouse.com/docs/sql-reference/data-types/datetime64) - [Enum](https://clickhouse.com/docs/sql-reference/data-types/enum) - [UUID](https://clickhouse.com/docs/sql-reference/data-types/uuid) - [IPv4](https://clickhouse.com/docs/sql-reference/data-types/ipv4) - [IPv6](https://clickhouse.com/docs/sql-reference/data-types/ipv6) - [Array(T)](https://clickhouse.com/docs/sql-reference/data-types/array) - [Boolean](https://clickhouse.com/docs/sql-reference/data-types/boolean) - [Tuple(T1, T2, ...)](https://clickhouse.com/docs/sql-reference/data-types/tuple) - [Map(K, V)](https://clickhouse.com/docs/sql-reference/data-types/map) - [Variant(T1, T2, ...)](https://clickhouse.com/docs/sql-reference/data-types/variant) - [LowCardinality(T)](https://clickhouse.com/docs/sql-reference/data-types/lowcardinality) - [Nullable(T)](https://clickhouse.com/docs/sql-reference/data-types/nullable) - [AggregateFunction](https://clickhouse.com/docs/sql-reference/data-types/aggregatefunction) - [SimpleAggregateFunction](https://clickhouse.com/docs/sql-reference/data-types/simpleaggregatefunction) - [Geo](https://clickhouse.com/docs/sql-reference/data-types/geo) - [Special Data Types](https://clickhouse.com/docs/sql-reference/data-types/special-data-types) - [Data types binary encoding specification.](https://clickhouse.com/docs/sql-reference/data-types/data-types-binary-encoding) - [Domains](https://clickhouse.com/docs/sql-reference/data-types/domains) - [Nested(Name1 Type1, Name2 Type2, ...)](https://clickhouse.com/docs/sql-reference/data-types/nested-data-structures/nested) - [Dynamic](https://clickhouse.com/docs/sql-reference/data-types/dynamic) - [JSON](https://clickhouse.com/docs/sql-reference/data-types/newjson) - [QBit](https://clickhouse.com/docs/sql-reference/data-types/qbit) - [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) - [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) - [Introduction](https://clickhouse.com/docs/sql-reference) - [Data types](https://clickhouse.com/docs/sql-reference/data-types) - AggregateFunction [Edit this page](https://github.com/ClickHouse/ClickHouse/tree/master/docs/en/sql-reference/data-types/aggregatefunction.md) # AggregateFunction Type ## Description[​](https://clickhouse.com/docs/sql-reference/data-types/aggregatefunction#description "Direct link to Description") All [Aggregate functions](https://clickhouse.com/docs/sql-reference/aggregate-functions) in ClickHouse have an implementation-specific intermediate state that can be serialized to an `AggregateFunction` data type and stored in a table. This is usually done by means of a [materialized view](https://clickhouse.com/docs/sql-reference/statements/create/view). There are two aggregate function [combinators](https://clickhouse.com/docs/sql-reference/aggregate-functions/combinators) commonly used with the `AggregateFunction` type: - The [`-State`](https://clickhouse.com/docs/sql-reference/aggregate-functions/combinators#-state) aggregate function combinator, which when appended to an aggregate function name, produces `AggregateFunction` intermediate states. - The [`-Merge`](https://clickhouse.com/docs/sql-reference/aggregate-functions/combinators#-merge) aggregate function combinator, which is used to get the final result of an aggregation from the intermediate states. ## Syntax[​](https://clickhouse.com/docs/sql-reference/data-types/aggregatefunction#syntax "Direct link to Syntax") ``` AggregateFunction(aggregate_function_name, types_of_arguments...) ``` **Parameters** - `aggregate_function_name` - The name of an aggregate function. If the function is parametric, then its parameters should be specified too. - `types_of_arguments` - The types of the aggregate function arguments. for example: ``` CREATE TABLE t ( column1 AggregateFunction(uniq, UInt64), column2 AggregateFunction(anyIf, String, UInt8), column3 AggregateFunction(quantiles(0.5, 0.9), UInt64) ) ENGINE = ... ``` ## Usage[​](https://clickhouse.com/docs/sql-reference/data-types/aggregatefunction#usage "Direct link to Usage") ### Data Insertion[​](https://clickhouse.com/docs/sql-reference/data-types/aggregatefunction#data-insertion "Direct link to Data Insertion") To insert data into a table with columns of type `AggregateFunction`, you can use `INSERT SELECT` with aggregate functions and the [`-State`](https://clickhouse.com/docs/sql-reference/aggregate-functions/combinators#-state) aggregate function combinator. For example, to insert into columns of type `AggregateFunction(uniq, UInt64)` and `AggregateFunction(quantiles(0.5, 0.9), UInt64)` you would use the following aggregate functions with combinators. ``` uniqState(UserID) quantilesState(0.5, 0.9)(SendTiming) ``` In contrast to functions `uniq` and `quantiles`, `uniqState` and `quantilesState` (with `-State` combinator appended) return the state, rather than the final value. In other words, they return a value of `AggregateFunction` type. In the results of the `SELECT` query, values of type `AggregateFunction` have implementation-specific binary representations for all of the ClickHouse output formats. There is a special Session level setting `aggregate_function_input_format` that allows to build state from the input values. It supports the following formats: - `state` - binary string with the serialized state (the default). If you dump data into, for example, the `TabSeparated` format with a `SELECT` query, then this dump can be loaded back using the `INSERT` query. - `value` - the format will expect a single value of the argument of the aggregate function, or in the case of multiple arguments, a tuple of them; that will be deserialized to form the relevant state - `array` - the format will expect an Array of values, as described in the values option above; all the elements of the array will be aggregated to form the state ### Data Selection[​](https://clickhouse.com/docs/sql-reference/data-types/aggregatefunction#data-selection "Direct link to Data Selection") When selecting data from `AggregatingMergeTree` table, use the `GROUP BY` clause and the same aggregate functions as for when you inserted the data, but use the [`-Merge`](https://clickhouse.com/docs/sql-reference/aggregate-functions/combinators#-merge) combinator. An aggregate function with the `-Merge` combinator appended to it takes a set of states, combines them, and returns the result of the complete data aggregation. For example, the following two queries return the same result: ``` SELECT uniq(UserID) FROM table SELECT uniqMerge(state) FROM (SELECT uniqState(UserID) AS state FROM table GROUP BY RegionID) ``` ## Usage Example[​](https://clickhouse.com/docs/sql-reference/data-types/aggregatefunction#usage-example "Direct link to Usage Example") See [AggregatingMergeTree](https://clickhouse.com/docs/engines/table-engines/mergetree-family/aggregatingmergetree) engine description. ## Related Content[​](https://clickhouse.com/docs/sql-reference/data-types/aggregatefunction#related-content "Direct link to Related Content") - Blog: [Using Aggregate Combinators in ClickHouse](https://clickhouse.com/blog/aggregate-functions-combinators-in-clickhouse-for-arrays-maps-and-states) - [MergeState](https://clickhouse.com/docs/sql-reference/aggregate-functions/combinators#-mergestate) combinator. - [State](https://clickhouse.com/docs/sql-reference/aggregate-functions/combinators#-state) combinator. [Previous Nullable(T)](https://clickhouse.com/docs/sql-reference/data-types/nullable) [Next SimpleAggregateFunction](https://clickhouse.com/docs/sql-reference/data-types/simpleaggregatefunction) - [Description](https://clickhouse.com/docs/sql-reference/data-types/aggregatefunction#description) - [Syntax](https://clickhouse.com/docs/sql-reference/data-types/aggregatefunction#syntax) - [Usage](https://clickhouse.com/docs/sql-reference/data-types/aggregatefunction#usage) - [Data Insertion](https://clickhouse.com/docs/sql-reference/data-types/aggregatefunction#data-insertion) - [Data Selection](https://clickhouse.com/docs/sql-reference/data-types/aggregatefunction#data-selection) - [Usage Example](https://clickhouse.com/docs/sql-reference/data-types/aggregatefunction#usage-example) - [Related Content](https://clickhouse.com/docs/sql-reference/data-types/aggregatefunction#related-content) 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=a0d5e403-d905-4068-8fc0-e2aae6645c9b&pagePath=%2Fdocs%2Fsql-reference%2Fdata-types%2Faggregatefunction&origPath=%2Fdocs%2Fsql-reference%2Fdata-types%2Faggregatefunction&utm_ga=GA1.1.1031461531.1776400579) © 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▼ - [Int \| UInt](https://clickhouse.com/docs/sql-reference/data-types/int-uint) - [Float32 \| Float64 \| BFloat16](https://clickhouse.com/docs/sql-reference/data-types/float) - [Decimal](https://clickhouse.com/docs/sql-reference/data-types/decimal) - [String](https://clickhouse.com/docs/sql-reference/data-types/string) - [FixedString(N)](https://clickhouse.com/docs/sql-reference/data-types/fixedstring) - [Date](https://clickhouse.com/docs/sql-reference/data-types/date) - [Date32](https://clickhouse.com/docs/sql-reference/data-types/date32) - [Time](https://clickhouse.com/docs/sql-reference/data-types/time) - [DateTime](https://clickhouse.com/docs/sql-reference/data-types/datetime) - [Time64](https://clickhouse.com/docs/sql-reference/data-types/time64) - [DateTime64](https://clickhouse.com/docs/sql-reference/data-types/datetime64) - [Enum](https://clickhouse.com/docs/sql-reference/data-types/enum) - [UUID](https://clickhouse.com/docs/sql-reference/data-types/uuid) - [IPv4](https://clickhouse.com/docs/sql-reference/data-types/ipv4) - [IPv6](https://clickhouse.com/docs/sql-reference/data-types/ipv6) - [Array(T)](https://clickhouse.com/docs/sql-reference/data-types/array) - [Boolean](https://clickhouse.com/docs/sql-reference/data-types/boolean) - [Tuple(T1, T2, ...)](https://clickhouse.com/docs/sql-reference/data-types/tuple) - [Map(K, V)](https://clickhouse.com/docs/sql-reference/data-types/map) - [Variant(T1, T2, ...)](https://clickhouse.com/docs/sql-reference/data-types/variant) - [LowCardinality(T)](https://clickhouse.com/docs/sql-reference/data-types/lowcardinality) - [Nullable(T)](https://clickhouse.com/docs/sql-reference/data-types/nullable) - [AggregateFunction](https://clickhouse.com/docs/sql-reference/data-types/aggregatefunction) - [SimpleAggregateFunction](https://clickhouse.com/docs/sql-reference/data-types/simpleaggregatefunction) - [Geo](https://clickhouse.com/docs/sql-reference/data-types/geo) - Special Data Types▼ - [Data types binary encoding specification.](https://clickhouse.com/docs/sql-reference/data-types/data-types-binary-encoding) - [Domains](https://clickhouse.com/docs/sql-reference/data-types/domains) - [Nested(Name1 Type1, Name2 Type2, ...)](https://clickhouse.com/docs/sql-reference/data-types/nested-data-structures/nested) - [Dynamic](https://clickhouse.com/docs/sql-reference/data-types/dynamic) - [JSON](https://clickhouse.com/docs/sql-reference/data-types/newjson) - [QBit](https://clickhouse.com/docs/sql-reference/data-types/qbit) - Statements▼ - Operators▼ - Engines▼ - Functions▼ - Formats▼ - [Data Lakes](https://clickhouse.com/docs/sql-reference/datalakes)
Readable Markdown
## Description[​](https://clickhouse.com/docs/sql-reference/data-types/aggregatefunction#description "Direct link to Description") All [Aggregate functions](https://clickhouse.com/docs/sql-reference/aggregate-functions) in ClickHouse have an implementation-specific intermediate state that can be serialized to an `AggregateFunction` data type and stored in a table. This is usually done by means of a [materialized view](https://clickhouse.com/docs/sql-reference/statements/create/view). There are two aggregate function [combinators](https://clickhouse.com/docs/sql-reference/aggregate-functions/combinators) commonly used with the `AggregateFunction` type: - The [`-State`](https://clickhouse.com/docs/sql-reference/aggregate-functions/combinators#-state) aggregate function combinator, which when appended to an aggregate function name, produces `AggregateFunction` intermediate states. - The [`-Merge`](https://clickhouse.com/docs/sql-reference/aggregate-functions/combinators#-merge) aggregate function combinator, which is used to get the final result of an aggregation from the intermediate states. ## Syntax[​](https://clickhouse.com/docs/sql-reference/data-types/aggregatefunction#syntax "Direct link to Syntax") ``` AggregateFunction(aggregate_function_name, types_of_arguments...) ``` **Parameters** - `aggregate_function_name` - The name of an aggregate function. If the function is parametric, then its parameters should be specified too. - `types_of_arguments` - The types of the aggregate function arguments. for example: ``` CREATE TABLE t ( column1 AggregateFunction(uniq, UInt64), column2 AggregateFunction(anyIf, String, UInt8), column3 AggregateFunction(quantiles(0.5, 0.9), UInt64) ) ENGINE = ... ``` ## Usage[​](https://clickhouse.com/docs/sql-reference/data-types/aggregatefunction#usage "Direct link to Usage") ### Data Insertion[​](https://clickhouse.com/docs/sql-reference/data-types/aggregatefunction#data-insertion "Direct link to Data Insertion") To insert data into a table with columns of type `AggregateFunction`, you can use `INSERT SELECT` with aggregate functions and the [`-State`](https://clickhouse.com/docs/sql-reference/aggregate-functions/combinators#-state) aggregate function combinator. For example, to insert into columns of type `AggregateFunction(uniq, UInt64)` and `AggregateFunction(quantiles(0.5, 0.9), UInt64)` you would use the following aggregate functions with combinators. ``` uniqState(UserID) quantilesState(0.5, 0.9)(SendTiming) ``` In contrast to functions `uniq` and `quantiles`, `uniqState` and `quantilesState` (with `-State` combinator appended) return the state, rather than the final value. In other words, they return a value of `AggregateFunction` type. In the results of the `SELECT` query, values of type `AggregateFunction` have implementation-specific binary representations for all of the ClickHouse output formats. There is a special Session level setting `aggregate_function_input_format` that allows to build state from the input values. It supports the following formats: - `state` - binary string with the serialized state (the default). If you dump data into, for example, the `TabSeparated` format with a `SELECT` query, then this dump can be loaded back using the `INSERT` query. - `value` - the format will expect a single value of the argument of the aggregate function, or in the case of multiple arguments, a tuple of them; that will be deserialized to form the relevant state - `array` - the format will expect an Array of values, as described in the values option above; all the elements of the array will be aggregated to form the state ### Data Selection[​](https://clickhouse.com/docs/sql-reference/data-types/aggregatefunction#data-selection "Direct link to Data Selection") When selecting data from `AggregatingMergeTree` table, use the `GROUP BY` clause and the same aggregate functions as for when you inserted the data, but use the [`-Merge`](https://clickhouse.com/docs/sql-reference/aggregate-functions/combinators#-merge) combinator. An aggregate function with the `-Merge` combinator appended to it takes a set of states, combines them, and returns the result of the complete data aggregation. For example, the following two queries return the same result: ``` SELECT uniq(UserID) FROM table SELECT uniqMerge(state) FROM (SELECT uniqState(UserID) AS state FROM table GROUP BY RegionID) ``` ## Usage Example[​](https://clickhouse.com/docs/sql-reference/data-types/aggregatefunction#usage-example "Direct link to Usage Example") See [AggregatingMergeTree](https://clickhouse.com/docs/engines/table-engines/mergetree-family/aggregatingmergetree) engine description. ## Related Content[​](https://clickhouse.com/docs/sql-reference/data-types/aggregatefunction#related-content "Direct link to Related Content") - Blog: [Using Aggregate Combinators in ClickHouse](https://clickhouse.com/blog/aggregate-functions-combinators-in-clickhouse-for-arrays-maps-and-states) - [MergeState](https://clickhouse.com/docs/sql-reference/aggregate-functions/combinators#-mergestate) combinator. - [State](https://clickhouse.com/docs/sql-reference/aggregate-functions/combinators#-state) combinator.
Shard89 (laksa)
Root Hash12633450985039531489
Unparsed URLcom,clickhouse!/docs/sql-reference/data-types/aggregatefunction s443