βΉοΈ 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/engines/table-engines/mergetree-family/aggregatingmergetree |
| Last Crawled | 2026-04-09 21:17:39 (2 days ago) |
| First Indexed | 2025-02-21 00:39:49 (1 year ago) |
| HTTP Status Code | 200 |
| Meta Title | AggregatingMergeTree table engine | ClickHouse Docs |
| Meta Description | Replaces all rows with the same primary key (or more accurately, with the same [sorting key](../../../engines/table-engines/mergetree-family/mergetree.md)) with a single row (within a single data part) that stores a combination of states of aggregate functions. |
| Meta Canonical | null |
| Boilerpipe Text | The engine inherits from
MergeTree
, altering the logic for data parts merging. ClickHouse replaces all rows with the same primary key (or more accurately, with the same
sorting key
) with a single row (within a single data part) that stores a combination of states of aggregate functions.
You can use
AggregatingMergeTree
tables for incremental data aggregation, including for aggregated materialized views.
You can see an example of how to use the AggregatingMergeTree and Aggregate functions in the below video:
The engine processes all columns with the following types:
AggregateFunction
SimpleAggregateFunction
It is appropriate to use
AggregatingMergeTree
if it reduces the number of rows by orders.
Creating a table
β
CREATE
TABLE
[
IF
NOT
EXISTS
]
[
db
.
]
table_name
[
ON
CLUSTER cluster
]
(
name1
[
type1
]
[
DEFAULT
|
MATERIALIZED
|
ALIAS expr1
]
,
name2
[
type2
]
[
DEFAULT
|
MATERIALIZED
|
ALIAS expr2
]
,
.
.
.
)
ENGINE
=
AggregatingMergeTree
(
)
[
PARTITION
BY
expr
]
[
ORDER
BY
expr
]
[
SAMPLE
BY
expr
]
[
TTL expr
]
[
SETTINGS name
=
value
,
.
.
.
]
For a description of request parameters, see
request description
.
Query clauses
When creating an
AggregatingMergeTree
table, the same
clauses
are required as when creating a
MergeTree
table.
Deprecated Method for Creating a Table
SELECT and INSERT
β
To insert data, use
INSERT SELECT
query with aggregate -State- functions.
When selecting data from
AggregatingMergeTree
table, use
GROUP BY
clause and the same aggregate functions as when inserting data, but using the
-Merge
suffix.
In the results of
SELECT
query, the values of
AggregateFunction
type have implementation-specific binary representation for all of the ClickHouse output formats. For example, if you dump data into
TabSeparated
format with a
SELECT
query, then this dump can be loaded back using an
INSERT
query.
Example of an aggregated materialized view
β
The following example assumes that you have a database named
test
. Create it if it doesn't already exist using the command below:
CREATE
DATABASE
test
;
Now create the table
test.visits
that contains the raw data:
CREATE
TABLE
test
.
visits
(
StartDate DateTime64
NOT
NULL
,
CounterID UInt64
,
Sign Nullable
(
Int32
)
,
UserID Nullable
(
Int32
)
)
ENGINE
=
MergeTree
ORDER
BY
(
StartDate
,
CounterID
)
;
Next, you need an
AggregatingMergeTree
table that will store
AggregationFunction
s that keep track of the total number of visits and the number of unique users.
Create an
AggregatingMergeTree
materialized view that watches the
test.visits
table, and uses the
AggregateFunction
type:
CREATE
TABLE
test
.
agg_visits
(
StartDate DateTime64
NOT
NULL
,
CounterID UInt64
,
Visits AggregateFunction
(
sum
,
Nullable
(
Int32
)
)
,
Users AggregateFunction
(
uniq
,
Nullable
(
Int32
)
)
)
ENGINE
=
AggregatingMergeTree
(
)
ORDER
BY
(
StartDate
,
CounterID
)
;
Create a materialized view that populates
test.agg_visits
from
test.visits
:
CREATE
MATERIALIZED
VIEW
test
.
visits_mv
TO
test
.
agg_visits
AS
SELECT
StartDate
,
CounterID
,
sumState
(
Sign
)
AS
Visits
,
uniqState
(
UserID
)
AS
Users
FROM
test
.
visits
GROUP
BY
StartDate
,
CounterID
;
Insert data into the
test.visits
table:
INSERT
INTO
test
.
visits
(
StartDate
,
CounterID
,
Sign
,
UserID
)
VALUES
(
1667446031000
,
1
,
3
,
4
)
,
(
1667446031000
,
1
,
6
,
3
)
;
The data is inserted in both
test.visits
and
test.agg_visits
.
To get the aggregated data, execute a query such as
SELECT ... GROUP BY ...
from the materialized view
test.visits_mv
:
SELECT
StartDate
,
sumMerge
(
Visits
)
AS
Visits
,
uniqMerge
(
Users
)
AS
Users
FROM
test
.
visits_mv
GROUP
BY
StartDate
ORDER
BY
StartDate
;
ββββββββββββββββStartDateββ¬βVisitsββ¬βUsersββ
β 2022-11-03 03:27:11.000 β 9 β 2 β
βββββββββββββββββββββββββββ΄βββββββββ΄ββββββββ
Add another couple of records to
test.visits
, but this time try using a different timestamp for one of the records:
INSERT
INTO
test
.
visits
(
StartDate
,
CounterID
,
Sign
,
UserID
)
VALUES
(
1669446031000
,
2
,
5
,
10
)
,
(
1667446031000
,
3
,
7
,
5
)
;
Run the
SELECT
query again, which will return the following output:
ββββββββββββββββStartDateββ¬βVisitsββ¬βUsersββ
β 2022-11-03 03:27:11.000 β 16 β 3 β
β 2022-11-26 07:00:31.000 β 5 β 1 β
βββββββββββββββββββββββββββ΄βββββββββ΄ββββββββ
In some cases, you might want to avoid pre-aggregating rows at insert time to shift the cost of aggregation from insert time
to merge time. Ordinarily, it is necessary to include the columns which are not part of the aggregation in the
GROUP BY
clause of the materialized view definition to avoid an error. However, you can make use of the
initializeAggregation
function with setting
optimize_on_insert = 0
(it is turned on by default) to achieve this. Use of
GROUP BY
is no longer required in this case:
CREATE
MATERIALIZED
VIEW
test
.
visits_mv
TO
test
.
agg_visits
AS
SELECT
StartDate
,
CounterID
,
initializeAggregation
(
'sumState'
,
Sign
)
AS
Visits
,
initializeAggregation
(
'uniqState'
,
UserID
)
AS
Users
FROM
test
.
visits
;
Note
When using
initializeAggregation
, an aggregate state is created for each individual row without grouping.
Each source row produces one row in the materialized view, and the actual aggregation happens later when the
AggregatingMergeTree
merges parts. This is only true if
optimize_on_insert = 0
.
Related content
β
Blog:
Using Aggregate Combinators in ClickHouse |
| Markdown | [Skip to main content](https://clickhouse.com/docs/engines/table-engines/mergetree-family/aggregatingmergetree#__docusaurus_skipToContent_fallback)
[](https://clickhouse.com/)
- [Products](https://clickhouse.com/docs/engines/table-engines/mergetree-family/aggregatingmergetree)
- [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/engines/table-engines/mergetree-family/aggregatingmergetree)
- [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/engines/table-engines/mergetree-family/aggregatingmergetree)
- [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/engines/table-engines/mergetree-family/aggregatingmergetree)
- [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=d7cca8d0-d518-41d8-b4ff-08301e0b31a4&pagePath=%2Fdocs%2Fengines%2Ftable-engines%2Fmergetree-family%2Faggregatingmergetree&origPath=%2Fdocs%2Fengines%2Ftable-engines%2Fmergetree-family%2Faggregatingmergetree&utm_ga=GA1.1.1947800976.1775769461)
[Get started](https://console.clickhouse.cloud/signUp?loc=docs-nav-signUp-cta&glxid=d7cca8d0-d518-41d8-b4ff-08301e0b31a4&pagePath=%2Fdocs%2Fengines%2Ftable-engines%2Fmergetree-family%2Faggregatingmergetree&origPath=%2Fdocs%2Fengines%2Ftable-engines%2Fmergetree-family%2Faggregatingmergetree&utm_ga=GA1.1.1947800976.1775769461)
[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/engines/table-engines/mergetree-family/aggregatingmergetree)
- [English](https://clickhouse.com/docs/engines/table-engines/mergetree-family/aggregatingmergetree)
- [ζ₯ζ¬θͺ](https://clickhouse.com/docs/jp/engines/table-engines/mergetree-family/aggregatingmergetree)
- [δΈζ](https://clickhouse.com/docs/zh/engines/table-engines/mergetree-family/aggregatingmergetree)
- [Π ΡΡΡΠΊΠΈΠΉ](https://clickhouse.com/docs/ru/engines/table-engines/mergetree-family/aggregatingmergetree)
- [νκ΅μ΄](https://clickhouse.com/docs/ko/engines/table-engines/mergetree-family/aggregatingmergetree)
[Skip to main content](https://clickhouse.com/docs/engines/table-engines/mergetree-family/aggregatingmergetree#__docusaurus_skipToContent_fallback)
[](https://clickhouse.com/)
- [Products](https://clickhouse.com/docs/engines/table-engines/mergetree-family/aggregatingmergetree)
- [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/engines/table-engines/mergetree-family/aggregatingmergetree)
- [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/engines/table-engines/mergetree-family/aggregatingmergetree)
- [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/engines/table-engines/mergetree-family/aggregatingmergetree)
- [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=d7cca8d0-d518-41d8-b4ff-08301e0b31a4&pagePath=%2Fdocs%2Fengines%2Ftable-engines%2Fmergetree-family%2Faggregatingmergetree&origPath=%2Fdocs%2Fengines%2Ftable-engines%2Fmergetree-family%2Faggregatingmergetree&utm_ga=GA1.1.1947800976.1775769461)
[Get started](https://console.clickhouse.cloud/signUp?loc=docs-nav-signUp-cta&glxid=d7cca8d0-d518-41d8-b4ff-08301e0b31a4&pagePath=%2Fdocs%2Fengines%2Ftable-engines%2Fmergetree-family%2Faggregatingmergetree&origPath=%2Fdocs%2Fengines%2Ftable-engines%2Fmergetree-family%2Faggregatingmergetree&utm_ga=GA1.1.1947800976.1775769461)
[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/engines/table-engines/mergetree-family/aggregatingmergetree)
- [English](https://clickhouse.com/docs/engines/table-engines/mergetree-family/aggregatingmergetree)
- [ζ₯ζ¬θͺ](https://clickhouse.com/docs/jp/engines/table-engines/mergetree-family/aggregatingmergetree)
- [δΈζ](https://clickhouse.com/docs/zh/engines/table-engines/mergetree-family/aggregatingmergetree)
- [Π ΡΡΡΠΊΠΈΠΉ](https://clickhouse.com/docs/ru/engines/table-engines/mergetree-family/aggregatingmergetree)
- [νκ΅μ΄](https://clickhouse.com/docs/ko/engines/table-engines/mergetree-family/aggregatingmergetree)
[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)
- [MergeTree Family](https://clickhouse.com/docs/engines/table-engines/mergetree-family)
- [MergeTree](https://clickhouse.com/docs/engines/table-engines/mergetree-family/mergetree)
- [Replicated\*](https://clickhouse.com/docs/engines/table-engines/mergetree-family/replication)
- [Custom Partitioning Key](https://clickhouse.com/docs/engines/table-engines/mergetree-family/custom-partitioning-key)
- [ReplacingMergeTree](https://clickhouse.com/docs/engines/table-engines/mergetree-family/replacingmergetree)
- [CoalescingMergeTree](https://clickhouse.com/docs/engines/table-engines/mergetree-family/coalescingmergetree)
- [SummingMergeTree](https://clickhouse.com/docs/engines/table-engines/mergetree-family/summingmergetree)
- [AggregatingMergeTree](https://clickhouse.com/docs/engines/table-engines/mergetree-family/aggregatingmergetree)
- [CollapsingMergeTree](https://clickhouse.com/docs/engines/table-engines/mergetree-family/collapsingmergetree)
- [VersionedCollapsingMergeTree](https://clickhouse.com/docs/engines/table-engines/mergetree-family/versionedcollapsingmergetree)
- [GraphiteMergeTree](https://clickhouse.com/docs/engines/table-engines/mergetree-family/graphitemergetree)
- [Exact and Approximate Vector Search](https://clickhouse.com/docs/engines/table-engines/mergetree-family/annindexes)
- [Full-text Search with Text Indexes](https://clickhouse.com/docs/engines/table-engines/mergetree-family/textindexes)
- [Log family](https://clickhouse.com/docs/engines/table-engines/log-family)
- [Integrations](https://clickhouse.com/docs/engines/table-engines/integrations)
- [Special](https://clickhouse.com/docs/engines/table-engines/special)
- [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)
- [Engines](https://clickhouse.com/docs/engines)
- [Table Engines](https://clickhouse.com/docs/engines/table-engines)
- [MergeTree Family](https://clickhouse.com/docs/engines/table-engines/mergetree-family)
- AggregatingMergeTree
[Edit this page](https://github.com/ClickHouse/ClickHouse/tree/master/docs/en/engines/table-engines/mergetree-family/aggregatingmergetree.md)
# AggregatingMergeTree table engine
The engine inherits from [MergeTree](https://clickhouse.com/docs/engines/table-engines/mergetree-family/versionedcollapsingmergetree), altering the logic for data parts merging. ClickHouse replaces all rows with the same primary key (or more accurately, with the same [sorting key](https://clickhouse.com/docs/engines/table-engines/mergetree-family/mergetree)) with a single row (within a single data part) that stores a combination of states of aggregate functions.
You can use `AggregatingMergeTree` tables for incremental data aggregation, including for aggregated materialized views.
You can see an example of how to use the AggregatingMergeTree and Aggregate functions in the below video:
The engine processes all columns with the following types:
- [`AggregateFunction`](https://clickhouse.com/docs/sql-reference/data-types/aggregatefunction)
- [`SimpleAggregateFunction`](https://clickhouse.com/docs/sql-reference/data-types/simpleaggregatefunction)
It is appropriate to use `AggregatingMergeTree` if it reduces the number of rows by orders.
## Creating a table[β](https://clickhouse.com/docs/engines/table-engines/mergetree-family/aggregatingmergetree#creating-a-table "Direct link to Creating a table")
```
CREATE TABLE [IF NOT EXISTS] [db.]table_name [ON CLUSTER cluster]
(
name1 [type1] [DEFAULT|MATERIALIZED|ALIAS expr1],
name2 [type2] [DEFAULT|MATERIALIZED|ALIAS expr2],
...
) ENGINE = AggregatingMergeTree()
[PARTITION BY expr]
[ORDER BY expr]
[SAMPLE BY expr]
[TTL expr]
[SETTINGS name=value, ...]
```
For a description of request parameters, see [request description](https://clickhouse.com/docs/sql-reference/statements/create/table).
**Query clauses**
When creating an `AggregatingMergeTree` table, the same [clauses](https://clickhouse.com/docs/engines/table-engines/mergetree-family/mergetree) are required as when creating a `MergeTree` table.
Deprecated Method for Creating a Table
Note
Do not use this method in new projects and, if possible, switch the old projects to the method described above.
```
CREATE TABLE [IF NOT EXISTS] [db.]table_name [ON CLUSTER cluster]
(
name1 [type1] [DEFAULT|MATERIALIZED|ALIAS expr1],
name2 [type2] [DEFAULT|MATERIALIZED|ALIAS expr2],
...
) ENGINE [=] AggregatingMergeTree(date-column [, sampling_expression], (primary, key), index_granularity)
```
All of the parameters have the same meaning as in `MergeTree`.
## SELECT and INSERT[β](https://clickhouse.com/docs/engines/table-engines/mergetree-family/aggregatingmergetree#select-and-insert "Direct link to SELECT and INSERT")
To insert data, use [INSERT SELECT](https://clickhouse.com/docs/sql-reference/statements/insert-into) query with aggregate -State- functions. When selecting data from `AggregatingMergeTree` table, use `GROUP BY` clause and the same aggregate functions as when inserting data, but using the `-Merge` suffix.
In the results of `SELECT` query, the values of `AggregateFunction` type have implementation-specific binary representation for all of the ClickHouse output formats. For example, if you dump data into `TabSeparated` format with a `SELECT` query, then this dump can be loaded back using an `INSERT` query.
## Example of an aggregated materialized view[β](https://clickhouse.com/docs/engines/table-engines/mergetree-family/aggregatingmergetree#example-of-an-aggregated-materialized-view "Direct link to Example of an aggregated materialized view")
The following example assumes that you have a database named `test`. Create it if it doesn't already exist using the command below:
```
CREATE DATABASE test;
```
Now create the table `test.visits` that contains the raw data:
```
CREATE TABLE test.visits
(
StartDate DateTime64 NOT NULL,
CounterID UInt64,
Sign Nullable(Int32),
UserID Nullable(Int32)
) ENGINE = MergeTree ORDER BY (StartDate, CounterID);
```
Next, you need an `AggregatingMergeTree` table that will store `AggregationFunction`s that keep track of the total number of visits and the number of unique users.
Create an `AggregatingMergeTree` materialized view that watches the `test.visits` table, and uses the [`AggregateFunction`](https://clickhouse.com/docs/sql-reference/data-types/aggregatefunction) type:
```
CREATE TABLE test.agg_visits (
StartDate DateTime64 NOT NULL,
CounterID UInt64,
Visits AggregateFunction(sum, Nullable(Int32)),
Users AggregateFunction(uniq, Nullable(Int32))
)
ENGINE = AggregatingMergeTree() ORDER BY (StartDate, CounterID);
```
Create a materialized view that populates `test.agg_visits` from `test.visits`:
```
CREATE MATERIALIZED VIEW test.visits_mv TO test.agg_visits
AS SELECT
StartDate,
CounterID,
sumState(Sign) AS Visits,
uniqState(UserID) AS Users
FROM test.visits
GROUP BY StartDate, CounterID;
```
Insert data into the `test.visits` table:
```
INSERT INTO test.visits (StartDate, CounterID, Sign, UserID)
VALUES (1667446031000, 1, 3, 4), (1667446031000, 1, 6, 3);
```
The data is inserted in both `test.visits` and `test.agg_visits`.
To get the aggregated data, execute a query such as `SELECT ... GROUP BY ...` from the materialized view `test.visits_mv`:
```
SELECT
StartDate,
sumMerge(Visits) AS Visits,
uniqMerge(Users) AS Users
FROM test.visits_mv
GROUP BY StartDate
ORDER BY StartDate;
```
```
ββββββββββββββββStartDateββ¬βVisitsββ¬βUsersββ
β 2022-11-03 03:27:11.000 β 9 β 2 β
βββββββββββββββββββββββββββ΄βββββββββ΄ββββββββ
```
Add another couple of records to `test.visits`, but this time try using a different timestamp for one of the records:
```
INSERT INTO test.visits (StartDate, CounterID, Sign, UserID)
VALUES (1669446031000, 2, 5, 10), (1667446031000, 3, 7, 5);
```
Run the `SELECT` query again, which will return the following output:
```
ββββββββββββββββStartDateββ¬βVisitsββ¬βUsersββ
β 2022-11-03 03:27:11.000 β 16 β 3 β
β 2022-11-26 07:00:31.000 β 5 β 1 β
βββββββββββββββββββββββββββ΄βββββββββ΄ββββββββ
```
In some cases, you might want to avoid pre-aggregating rows at insert time to shift the cost of aggregation from insert time to merge time. Ordinarily, it is necessary to include the columns which are not part of the aggregation in the `GROUP BY` clause of the materialized view definition to avoid an error. However, you can make use of the [`initializeAggregation`](https://clickhouse.com/docs/sql-reference/functions/other-functions#initializeAggregation) function with setting `optimize_on_insert = 0` (it is turned on by default) to achieve this. Use of `GROUP BY` is no longer required in this case:
```
CREATE MATERIALIZED VIEW test.visits_mv TO test.agg_visits
AS SELECT
StartDate,
CounterID,
initializeAggregation('sumState', Sign) AS Visits,
initializeAggregation('uniqState', UserID) AS Users
FROM test.visits;
```
Note
When using `initializeAggregation`, an aggregate state is created for each individual row without grouping. Each source row produces one row in the materialized view, and the actual aggregation happens later when the `AggregatingMergeTree` merges parts. This is only true if `optimize_on_insert = 0`.
## Related content[β](https://clickhouse.com/docs/engines/table-engines/mergetree-family/aggregatingmergetree#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)
[Previous SummingMergeTree](https://clickhouse.com/docs/engines/table-engines/mergetree-family/summingmergetree)
[Next CollapsingMergeTree](https://clickhouse.com/docs/engines/table-engines/mergetree-family/collapsingmergetree)
- [Creating a table](https://clickhouse.com/docs/engines/table-engines/mergetree-family/aggregatingmergetree#creating-a-table)
- [SELECT and INSERT](https://clickhouse.com/docs/engines/table-engines/mergetree-family/aggregatingmergetree#select-and-insert)
- [Example of an aggregated materialized view](https://clickhouse.com/docs/engines/table-engines/mergetree-family/aggregatingmergetree#example-of-an-aggregated-materialized-view)
- [Related content](https://clickhouse.com/docs/engines/table-engines/mergetree-family/aggregatingmergetree#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=d7cca8d0-d518-41d8-b4ff-08301e0b31a4&pagePath=%2Fdocs%2Fengines%2Ftable-engines%2Fmergetree-family%2Faggregatingmergetree&origPath=%2Fdocs%2Fengines%2Ftable-engines%2Fmergetree-family%2Faggregatingmergetree&utm_ga=GA1.1.1947800976.1775769461)
Β© 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βΌ
- EnginesβΌ
- Database EnginesβΌ
- Table EnginesβΌ
- MergeTree FamilyβΌ
- [MergeTree](https://clickhouse.com/docs/engines/table-engines/mergetree-family/mergetree)
- [Replicated\*](https://clickhouse.com/docs/engines/table-engines/mergetree-family/replication)
- [Custom Partitioning Key](https://clickhouse.com/docs/engines/table-engines/mergetree-family/custom-partitioning-key)
- [ReplacingMergeTree](https://clickhouse.com/docs/engines/table-engines/mergetree-family/replacingmergetree)
- [CoalescingMergeTree](https://clickhouse.com/docs/engines/table-engines/mergetree-family/coalescingmergetree)
- [SummingMergeTree](https://clickhouse.com/docs/engines/table-engines/mergetree-family/summingmergetree)
- [AggregatingMergeTree](https://clickhouse.com/docs/engines/table-engines/mergetree-family/aggregatingmergetree)
- [CollapsingMergeTree](https://clickhouse.com/docs/engines/table-engines/mergetree-family/collapsingmergetree)
- [VersionedCollapsingMergeTree](https://clickhouse.com/docs/engines/table-engines/mergetree-family/versionedcollapsingmergetree)
- [GraphiteMergeTree](https://clickhouse.com/docs/engines/table-engines/mergetree-family/graphitemergetree)
- [Exact and Approximate Vector Search](https://clickhouse.com/docs/engines/table-engines/mergetree-family/annindexes)
- [Full-text Search with Text Indexes](https://clickhouse.com/docs/engines/table-engines/mergetree-family/textindexes)
- Log familyβΌ
- IntegrationsβΌ
- SpecialβΌ
- FunctionsβΌ
- FormatsβΌ
- [Data Lakes](https://clickhouse.com/docs/sql-reference/datalakes) |
| Readable Markdown | The engine inherits from [MergeTree](https://clickhouse.com/docs/engines/table-engines/mergetree-family/versionedcollapsingmergetree), altering the logic for data parts merging. ClickHouse replaces all rows with the same primary key (or more accurately, with the same [sorting key](https://clickhouse.com/docs/engines/table-engines/mergetree-family/mergetree)) with a single row (within a single data part) that stores a combination of states of aggregate functions.
You can use `AggregatingMergeTree` tables for incremental data aggregation, including for aggregated materialized views.
You can see an example of how to use the AggregatingMergeTree and Aggregate functions in the below video:
The engine processes all columns with the following types:
- [`AggregateFunction`](https://clickhouse.com/docs/sql-reference/data-types/aggregatefunction)
- [`SimpleAggregateFunction`](https://clickhouse.com/docs/sql-reference/data-types/simpleaggregatefunction)
It is appropriate to use `AggregatingMergeTree` if it reduces the number of rows by orders.
## Creating a table[β](https://clickhouse.com/docs/engines/table-engines/mergetree-family/aggregatingmergetree#creating-a-table "Direct link to Creating a table")
```
CREATE TABLE [IF NOT EXISTS] [db.]table_name [ON CLUSTER cluster]
(
name1 [type1] [DEFAULT|MATERIALIZED|ALIAS expr1],
name2 [type2] [DEFAULT|MATERIALIZED|ALIAS expr2],
...
) ENGINE = AggregatingMergeTree()
[PARTITION BY expr]
[ORDER BY expr]
[SAMPLE BY expr]
[TTL expr]
[SETTINGS name=value, ...]
```
For a description of request parameters, see [request description](https://clickhouse.com/docs/sql-reference/statements/create/table).
**Query clauses**
When creating an `AggregatingMergeTree` table, the same [clauses](https://clickhouse.com/docs/engines/table-engines/mergetree-family/mergetree) are required as when creating a `MergeTree` table.
Deprecated Method for Creating a Table
## SELECT and INSERT[β](https://clickhouse.com/docs/engines/table-engines/mergetree-family/aggregatingmergetree#select-and-insert "Direct link to SELECT and INSERT")
To insert data, use [INSERT SELECT](https://clickhouse.com/docs/sql-reference/statements/insert-into) query with aggregate -State- functions. When selecting data from `AggregatingMergeTree` table, use `GROUP BY` clause and the same aggregate functions as when inserting data, but using the `-Merge` suffix.
In the results of `SELECT` query, the values of `AggregateFunction` type have implementation-specific binary representation for all of the ClickHouse output formats. For example, if you dump data into `TabSeparated` format with a `SELECT` query, then this dump can be loaded back using an `INSERT` query.
## Example of an aggregated materialized view[β](https://clickhouse.com/docs/engines/table-engines/mergetree-family/aggregatingmergetree#example-of-an-aggregated-materialized-view "Direct link to Example of an aggregated materialized view")
The following example assumes that you have a database named `test`. Create it if it doesn't already exist using the command below:
```
CREATE DATABASE test;
```
Now create the table `test.visits` that contains the raw data:
```
CREATE TABLE test.visits
(
StartDate DateTime64 NOT NULL,
CounterID UInt64,
Sign Nullable(Int32),
UserID Nullable(Int32)
) ENGINE = MergeTree ORDER BY (StartDate, CounterID);
```
Next, you need an `AggregatingMergeTree` table that will store `AggregationFunction`s that keep track of the total number of visits and the number of unique users.
Create an `AggregatingMergeTree` materialized view that watches the `test.visits` table, and uses the [`AggregateFunction`](https://clickhouse.com/docs/sql-reference/data-types/aggregatefunction) type:
```
CREATE TABLE test.agg_visits (
StartDate DateTime64 NOT NULL,
CounterID UInt64,
Visits AggregateFunction(sum, Nullable(Int32)),
Users AggregateFunction(uniq, Nullable(Int32))
)
ENGINE = AggregatingMergeTree() ORDER BY (StartDate, CounterID);
```
Create a materialized view that populates `test.agg_visits` from `test.visits`:
```
CREATE MATERIALIZED VIEW test.visits_mv TO test.agg_visits
AS SELECT
StartDate,
CounterID,
sumState(Sign) AS Visits,
uniqState(UserID) AS Users
FROM test.visits
GROUP BY StartDate, CounterID;
```
Insert data into the `test.visits` table:
```
INSERT INTO test.visits (StartDate, CounterID, Sign, UserID)
VALUES (1667446031000, 1, 3, 4), (1667446031000, 1, 6, 3);
```
The data is inserted in both `test.visits` and `test.agg_visits`.
To get the aggregated data, execute a query such as `SELECT ... GROUP BY ...` from the materialized view `test.visits_mv`:
```
SELECT
StartDate,
sumMerge(Visits) AS Visits,
uniqMerge(Users) AS Users
FROM test.visits_mv
GROUP BY StartDate
ORDER BY StartDate;
```
```
ββββββββββββββββStartDateββ¬βVisitsββ¬βUsersββ
β 2022-11-03 03:27:11.000 β 9 β 2 β
βββββββββββββββββββββββββββ΄βββββββββ΄ββββββββ
```
Add another couple of records to `test.visits`, but this time try using a different timestamp for one of the records:
```
INSERT INTO test.visits (StartDate, CounterID, Sign, UserID)
VALUES (1669446031000, 2, 5, 10), (1667446031000, 3, 7, 5);
```
Run the `SELECT` query again, which will return the following output:
```
ββββββββββββββββStartDateββ¬βVisitsββ¬βUsersββ
β 2022-11-03 03:27:11.000 β 16 β 3 β
β 2022-11-26 07:00:31.000 β 5 β 1 β
βββββββββββββββββββββββββββ΄βββββββββ΄ββββββββ
```
In some cases, you might want to avoid pre-aggregating rows at insert time to shift the cost of aggregation from insert time to merge time. Ordinarily, it is necessary to include the columns which are not part of the aggregation in the `GROUP BY` clause of the materialized view definition to avoid an error. However, you can make use of the [`initializeAggregation`](https://clickhouse.com/docs/sql-reference/functions/other-functions#initializeAggregation) function with setting `optimize_on_insert = 0` (it is turned on by default) to achieve this. Use of `GROUP BY` is no longer required in this case:
```
CREATE MATERIALIZED VIEW test.visits_mv TO test.agg_visits
AS SELECT
StartDate,
CounterID,
initializeAggregation('sumState', Sign) AS Visits,
initializeAggregation('uniqState', UserID) AS Users
FROM test.visits;
```
Note
When using `initializeAggregation`, an aggregate state is created for each individual row without grouping. Each source row produces one row in the materialized view, and the actual aggregation happens later when the `AggregatingMergeTree` merges parts. This is only true if `optimize_on_insert = 0`.
## Related content[β](https://clickhouse.com/docs/engines/table-engines/mergetree-family/aggregatingmergetree#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) |
| Shard | 89 (laksa) |
| Root Hash | 12633450985039531489 |
| Unparsed URL | com,clickhouse!/docs/engines/table-engines/mergetree-family/aggregatingmergetree s443 |