Skip to main content

Using Filtered Aggregates in ClickHouse

Learn how to use filtered aggregates in ClickHouse with `-If` and `-Distinct` aggregate combinators to simplify query syntax and enhance analytics.

Using Filtered Aggregates

ClickHouse provides a simple and intuitive way to write filtered aggregates.

For example, compare the standard SQL way to write filtered aggregates (which work fine in ClickHouse) with the shorthand syntax using the -If aggregate function combinator, which can be appended to any aggregate function:

Similarly, there is a -Distinct aggregate combinator:

Why are filtered aggregates are important? Because they allow you to implement the "segment comparison" feature in web analytics services. For example:

Check out the aggregate function combinator page in the docs for more details.

· One min read