# max_simple_state

`max_simple_state` calculates a partial max and returns a directly readable scalar wrapped as SimpleAggregateFunction. Ordinary `max` can combine those partial values again.

Unlike a full AggregateFunction state, the scalar does not need `_merge` or `finalize_aggregation` before it can be read.

## Prepare two batches

```sql
CREATE TABLE readings(batch UInt8, value Int64);
INSERT INTO readings VALUES (1, 10), (1, 20), (2, 90);
CREATE TABLE partials(batch UInt8, result SimpleAggregateFunction(max, Int64));
INSERT INTO partials
SELECT batch, max_simple_state(value)
FROM readings
GROUP BY batch;
```

## Read partial results

```sql
SELECT batch, result
FROM partials
ORDER BY batch
FORMAT csv;
```

```text
1,20
2,90
```

## Combine the scalars

```sql
SELECT max(result)
FROM partials
FORMAT csv;
```

```text
90
```

The table does not combine rows automatically when values are inserted. Use an explicit aggregate query. See [SimpleAggregateFunction](https://coordinatordb.com/docs/reference/data-types/simpleaggregatefunction) for the supported inner types and functions.
