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
SimpleAggregateFunction(function, T)function—sum,minormax.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
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:
SELECT batch, total
FROM partial_totals
ORDER BY batch
FORMAT csv;1,30
2,90Combine them with ordinary sum:
SELECT sum(total)
FROM partial_totals
FORMAT csv;120Inserting 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.