Skip to content

SimpleAggregateFunction ​

SimpleAggregateFunction stores an aggregate's scalar partial result. The value can be read directly and combined with the ordinary aggregate again, without an intermediate-state merge or finalization call.

This works for sum, minimum and maximum: summing partial sums, for example, gives the sum of all original rows. An average needs both a total and a count, so use AggregateFunction for it.

Syntax ​

text
SimpleAggregateFunction(function, T)
  • function — sum, min or max.
  • T — The scalar value type. Assigned values must satisfy this inner type's constraints.

Construct values with sum_simple_state, min_simple_state or max_simple_state, or write plain scalar values to the declared column.

Example ​

Reading and combining partial totals ​

sql
CREATE TABLE partial_totals
(
    batch UInt8,
    total SimpleAggregateFunction(sum, UInt64)
);
INSERT INTO partial_totals VALUES (1, 30), (2, 90);

The values can be selected directly:

sql
SELECT batch, total
FROM partial_totals
ORDER BY batch
FORMAT csv;
text
1,30
2,90

Combine them with ordinary sum:

sql
SELECT sum(total)
FROM partial_totals
FORMAT csv;
text
120

Inserting rows does not automatically merge rows that have the same batch. Use an explicit aggregate query when you need a combined result.

See aggregate combinators for the _simple_state constructors.