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

# 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.

{frontMatter.description}

<h2 id="using-filtered-aggregates">
  Using Filtered Aggregates
</h2>

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](/reference/functions/aggregate-functions/combinators), which can be appended to any aggregate function:

```sql theme={null}
--standard SQL
SELECT
   avg(number)
FILTER (WHERE number > 50)
FROM numbers(100)

--ClickHouse using an aggregate combinator
SELECT
   avgIf(number, number > 50)
FROM numbers(100)
```

Similarly, there is a `-Distinct` aggregate combinator:

```sql theme={null}
--standard SQL
SELECT avg(DISTINCT number)

--ClickHouse using an aggregate combinator
SELECT avgDistinct(number)
```

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

```sql theme={null}
WITH
   Region = 'us' AS segment1,
   Browser = 'Chrome' AS segment2
SELECT
   uniqIf(UserID, segment1),
   uniqIf(UserID, segment2)
WHERE segment1 OR segment2
```

Check out the [aggregate function combinator](/reference/functions/aggregate-functions/combinators) page in the docs
for more details.
