> ## Documentation Index
> Fetch the complete documentation index at: https://private-7c7dfe99-mintlify-8c05c8a2.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

> Documentation for the AggregateFunction data type in ClickHouse, which stores intermediate states of aggregate functions

# AggregateFunction Type

<h2 id="description">
  Description
</h2>

All [Aggregate functions](/reference/functions/aggregate-functions/index) 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](/reference/statements/create/view).

There are two aggregate function [combinators](/reference/functions/aggregate-functions/combinators)
commonly used with the `AggregateFunction` type:

* The [`-State`](/reference/functions/aggregate-functions/combinators#-state) aggregate function combinator, which when appended to an aggregate
  function name, produces `AggregateFunction` intermediate states.
* The [`-Merge`](/reference/functions/aggregate-functions/combinators#-merge) aggregate
  function combinator, which is used to get the final result of an aggregation
  from the intermediate states.

<h2 id="syntax">
  Syntax
</h2>

```sql theme={null}
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:

```sql theme={null}
CREATE TABLE t
(
    column1 AggregateFunction(uniq, UInt64),
    column2 AggregateFunction(anyIf, String, UInt8),
    column3 AggregateFunction(quantiles(0.5, 0.9), UInt64)
) ENGINE = ...
```

<h2 id="usage">
  Usage
</h2>

<h3 id="data-insertion">
  Data Insertion
</h3>

To insert data into a table with columns of type `AggregateFunction`, you can
use `INSERT SELECT` with aggregate functions and the
[`-State`](/reference/functions/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.

```sql theme={null}
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

<h3 id="data-selection">
  Data Selection
</h3>

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`](/reference/functions/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:

```sql theme={null}
SELECT uniq(UserID) FROM table

SELECT uniqMerge(state) FROM (SELECT uniqState(UserID) AS state FROM table GROUP BY RegionID)
```

<h2 id="usage-example">
  Usage Example
</h2>

See [AggregatingMergeTree](/reference/engines/table-engines/mergetree-family/aggregatingmergetree) engine description.

<h2 id="related-content">
  Related Content
</h2>

* Blog: [Using Aggregate Combinators in ClickHouse](https://clickhouse.com/blog/aggregate-functions-combinators-in-clickhouse-for-arrays-maps-and-states)
* [MergeState](/reference/functions/aggregate-functions/combinators#-mergestate)
  combinator.
* [State](/reference/functions/aggregate-functions/combinators#-state) combinator.
