Skip to content

Aggregate combinators examples #3446

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 30 commits into
base: main
Choose a base branch
from

Conversation

Blargian
Copy link
Member

@Blargian Blargian commented Mar 6, 2025

Summary

Adds examples of common aggregate function combinator functions.

Checklist

Copy link

vercel bot commented Apr 21, 2025

@Blargian is attempting to deploy a commit to the ClickHouse Team on Vercel.

A member of the Team first needs to authorize it.

@Blargian Blargian marked this pull request as ready for review April 21, 2025 19:03
@Blargian Blargian requested a review from a team as a code owner April 21, 2025 19:03
Copy link

vercel bot commented Apr 22, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
clickhouse-docs ✅ Ready (Inspect) Visit Preview Apr 22, 2025 8:07am
2 Skipped Deployments
Name Status Preview Comments Updated (UTC)
clickhouse-docs-ru ⬜️ Ignored (Inspect) Visit Preview Apr 22, 2025 8:07am
clickhouse-docs-zh ⬜️ Ignored (Inspect) Visit Preview Apr 22, 2025 8:07am

FROM sales;
```

The `avgIf` function will calculate the average amount only for rows where `is_successful = 1`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems out of place

```

Let's get the average wage of the people whose age lies in the intervals of `[30,60)`
and `[60,75)`. Since we use integer representation for age, we get ages in the
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

] not )

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is ) meant to imply inclusive?

('Brian', 60, 16.0);
```

Let's count all the people whose age lies in the intervals of `[30,60)`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok i assume same as above

GROUP BY URLDomain
ORDER BY URLDomain ASC
LIMIT 20;
```
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add to sql.clickhouse.com repo please!


The [`SimpleState`](/sql-reference/aggregate-functions/combinators#-simplestate) combinator can be applied to the [`min`](/sql-reference/aggregate-functions/reference/min)
function to return the minimum value across all input values. It returns the
result with type `SimpleAggregateState`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

link to SimpleAggregateState and maybe say we simple is useful (i.e. its lighter than full aggregatestate but limited to certain functions)

```sql
SELECT
post_id,
sumSimpleState(upvotes) AS total_upvotes,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this seems wrong

Create a materialized view with `SimpleAggregateFunction` type columns:

```sql
CREATE MATERIALIZED VIEW vote_totals
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

create aggregate table seperately from MV for best practice

SELECT
post_id,
sumSimpleState(upvotes) AS total_upvotes,
sumSimpleState(downvotes) AS total_downvotes,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure we need these sumSimpleState

When you use `sumSimpleState(column)`:
- It directly extracts the stored sum value without doing any additional
aggregation.
- The result is of type `SimpleAggregateFunction(sum, Int64)`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this doesnt make sense, why do this?

AS
SELECT
post_id,
toUInt32(vote_type = 'upvote') AS upvotes,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

state here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants