updated finalizeAggregation

This commit is contained in:
George 2020-12-23 04:24:05 +03:00
parent 7ac6283315
commit d1c0f5f157

View File

@ -1172,7 +1172,100 @@ Result:
## finalizeAggregation {#function-finalizeaggregation}
Takes state of aggregate function. Returns result of aggregation (finalized state).
Takes [state](../../sql-reference/aggregate-functions/combinators/#agg-functions-combinator-state) of aggregate function. Returns result of aggregation (finalized state).
**Syntax**
``` sql
finalizeAggregation(state)
```
**Parameters**
- `state` — State of aggregation. [AggregateFunction](../../sql-reference/data-types/aggregatefunction.md#data-type-aggregatefunction).
**Returned value(s)**
- Value/values that was aggregated.
Type: Value of any types that was aggregated. [See](../../sql-reference/data-types/aggregatefunction/)
**Examples**
Query:
```sql
SELECT finalizeAggregation(( SELECT countState(number) FROM numbers(10)));
```
Result:
```text
┌─finalizeAggregation(_subquery16)─┐
│ 10 │
└──────────────────────────────────┘
```
Query:
```sql
SELECT finalizeAggregation(( SELECT sumState(number) FROM numbers(10)));
```
Result:
```text
┌─finalizeAggregation(_subquery20)─┐
│ 45 │
└──────────────────────────────────┘
```
Note that NULL values are ignored.
Query:
```sql
SELECT finalizeAggregation(arrayReduce('anyState', [NULL, 2, 3]));
```
Result:
```text
┌─finalizeAggregation(arrayReduce('anyState', [NULL, 2, 3]))─┐
│ 2 │
└────────────────────────────────────────────────────────────┘
```
Combined example:
Query:
```sql
WITH initializeAggregation('sumState', number) AS one_row_sum_state
SELECT
number,
finalizeAggregation(one_row_sum_state) AS one_row_sum,
runningAccumulate(one_row_sum_state) AS cumulative_sum
FROM numbers(10)
```
Result:
```text
┌─number─┬─one_row_sum─┬─cumulative_sum─┐
│ 0 │ 0 │ 0 │
│ 1 │ 1 │ 1 │
│ 2 │ 2 │ 3 │
│ 3 │ 3 │ 6 │
│ 4 │ 4 │ 10 │
│ 5 │ 5 │ 15 │
│ 6 │ 6 │ 21 │
│ 7 │ 7 │ 28 │
│ 8 │ 8 │ 36 │
│ 9 │ 9 │ 45 │
└────────┴─────────────┴────────────────┘
```
## runningAccumulate {#runningaccumulate}