> Documentation index: https://firebase.google.com/docs/reference/llms.txt

# AggregateFunction


```
public final class AggregateFunction
```

<br />

*** ** * ** ***

A class that represents an aggregate function.

## Summary

| ### Nested types |
|---|
| `public static class AggregateFunction.Companion` |

| ### Public methods |
|---|---|
| `final @NonNull AliasedAggregate` | `alias(@NonNull String alias)` Assigns an alias to this aggregate. |
| `static final @NonNull AggregateFunction` | `arrayAgg(@NonNull Expression expression)` Creates an aggregation that collects all values of an expression across multiple stage inputs into an array. |
| `static final @NonNull AggregateFunction` | `arrayAgg(@NonNull String fieldName)` Creates an aggregation that collects all values of a field across multiple stage inputs into an array. |
| `static final @NonNull AggregateFunction` | `arrayAggDistinct(@NonNull Expression expression)` Creates an aggregation that collects all distinct values of an expression across multiple stage inputs into an array. |
| `static final @NonNull AggregateFunction` | `arrayAggDistinct(@NonNull String fieldName)` Creates an aggregation that collects all distinct values of a field across multiple stage inputs into an array. |
| `static final @NonNull AggregateFunction` | `average(@NonNull Expression expression)` Creates an aggregation that calculates the average (mean) of values from an expression across multiple stage inputs. |
| `static final @NonNull AggregateFunction` | `average(@NonNull String fieldName)` Creates an aggregation that calculates the average (mean) of a field's values across multiple stage inputs. |
| `static final @NonNull AggregateFunction` | `count(@NonNull Expression expression)` Creates an aggregation that counts the number of stage inputs with valid evaluations of the provided `expression`. |
| `static final @NonNull AggregateFunction` | `count(@NonNull String fieldName)` Creates an aggregation that counts the number of stage inputs where the input field exists. |
| `static final @NonNull AggregateFunction` | `countAll()` Creates an aggregation that counts the total number of stage inputs. |
| `static final @NonNull AggregateFunction` | `countDistinct(@NonNull Expression expression)` Creates an aggregation that counts the number of distinct values of an expression across multiple stage inputs. |
| `static final @NonNull AggregateFunction` | `countDistinct(@NonNull String fieldName)` Creates an aggregation that counts the number of distinct values of a field across multiple stage inputs. |
| `static final @NonNull AggregateFunction` | `countIf(@NonNull BooleanExpression condition)` Creates an aggregation that counts the number of stage inputs where the provided boolean expression evaluates to true. |
| `static final @NonNull AggregateFunction` | `first(@NonNull Expression expression)` Creates an aggregation that finds the first value of an expression across multiple stage inputs. |
| `static final @NonNull AggregateFunction` | `first(@NonNull String fieldName)` Creates an aggregation that finds the first value of a field across multiple stage inputs. |
| `static final @NonNull AggregateFunction` | `last(@NonNull Expression expression)` Creates an aggregation that finds the last value of an expression across multiple stage inputs. |
| `static final @NonNull AggregateFunction` | `last(@NonNull String fieldName)` Creates an aggregation that finds the last value of a field across multiple stage inputs. |
| `static final @NonNull AggregateFunction` | `maximum(@NonNull Expression expression)` Creates an aggregation that finds the maximum value of an expression across multiple stage inputs. |
| `static final @NonNull AggregateFunction` | `maximum(@NonNull String fieldName)` Creates an aggregation that finds the maximum value of a field across multiple stage inputs. |
| `static final @NonNull AggregateFunction` | `minimum(@NonNull Expression expression)` Creates an aggregation that finds the minimum value of an expression across multiple stage inputs. |
| `static final @NonNull AggregateFunction` | `minimum(@NonNull String fieldName)` Creates an aggregation that finds the minimum value of a field across multiple stage inputs. |
| `final @NonNull WindowFunction` | `@Beta over(@NonNull WindowSpec window)` Applies a window frame to this aggregate, turning it into a window function. |
| `static final @NonNull AggregateFunction` | `rawAggregate(@NonNull String name, @NonNull Expression expr)` Creates a raw aggregation function. |
| `static final @NonNull AggregateFunction` | `sum(@NonNull Expression expression)` Creates an aggregation that calculates the sum of values from an expression across multiple stage inputs. |
| `static final @NonNull AggregateFunction` | `sum(@NonNull String fieldName)` Creates an aggregation that calculates the sum of a field's values across multiple stage inputs. |

## Public methods

### alias

```
public final @NonNull AliasedAggregate alias(@NonNull String alias)
```

Assigns an alias to this aggregate.

| Parameters |
|---|---|
| `@NonNull String alias` | The alias to assign to this aggregate. |

| Returns |
|---|---|
| `@NonNull AliasedAggregate` | A new `AliasedAggregate` that wraps this aggregate and associates it with the provided alias. |

### arrayAgg

```
public static final @NonNull AggregateFunction arrayAgg(@NonNull Expression expression)
```

Creates an aggregation that collects all values of an expression across multiple stage inputs into an array.

If the expression resolves to an absent value, it is converted to `null`. The order of elements in the output array is not stable and shouldn't be relied upon.

| Parameters |
|---|---|
| `@NonNull Expression expression` | The expression to collect values from. |

| Returns |
|---|---|
| `@NonNull AggregateFunction` | A new `AggregateFunction` representing the array_agg aggregation. |

### arrayAgg

```
public static final @NonNull AggregateFunction arrayAgg(@NonNull String fieldName)
```

Creates an aggregation that collects all values of a field across multiple stage inputs into an array.

If the expression resolves to an absent value, it is converted to `null`. The order of elements in the output array is not stable and shouldn't be relied upon.

| Parameters |
|---|---|
| `@NonNull String fieldName` | The name of the field to collect values from. |

| Returns |
|---|---|
| `@NonNull AggregateFunction` | A new `AggregateFunction` representing the array_agg aggregation. |

### arrayAggDistinct

```
public static final @NonNull AggregateFunction arrayAggDistinct(@NonNull Expression expression)
```

Creates an aggregation that collects all distinct values of an expression across multiple stage inputs into an array.

If the expression resolves to an absent value, it is converted to `null`. The order of elements in the output array is not stable and shouldn't be relied upon.

| Parameters |
|---|---|
| `@NonNull Expression expression` | The expression to collect values from. |

| Returns |
|---|---|
| `@NonNull AggregateFunction` | A new `AggregateFunction` representing the array_agg_distinct aggregation. |

### arrayAggDistinct

```
public static final @NonNull AggregateFunction arrayAggDistinct(@NonNull String fieldName)
```

Creates an aggregation that collects all distinct values of a field across multiple stage inputs into an array.

If the expression resolves to an absent value, it is converted to `null`. The order of elements in the output array is not stable and shouldn't be relied upon.

| Parameters |
|---|---|
| `@NonNull String fieldName` | The name of the field to collect values from. |

| Returns |
|---|---|
| `@NonNull AggregateFunction` | A new `AggregateFunction` representing the array_agg_distinct aggregation. |

### average

```
public static final @NonNull AggregateFunction average(@NonNull Expression expression)
```

Creates an aggregation that calculates the average (mean) of values from an expression across multiple stage inputs.

| Parameters |
|---|---|
| `@NonNull Expression expression` | The expression representing the values to average. |

| Returns |
|---|---|
| `@NonNull AggregateFunction` | A new `AggregateFunction` representing the average aggregation. |

### average

```
public static final @NonNull AggregateFunction average(@NonNull String fieldName)
```

Creates an aggregation that calculates the average (mean) of a field's values across multiple stage inputs.

| Parameters |
|---|---|
| `@NonNull String fieldName` | The name of the field containing numeric values to average. |

| Returns |
|---|---|
| `@NonNull AggregateFunction` | A new `AggregateFunction` representing the average aggregation. |

### count

```
public static final @NonNull AggregateFunction count(@NonNull Expression expression)
```

Creates an aggregation that counts the number of stage inputs with valid evaluations of the provided `expression`.

| Parameters |
|---|---|
| `@NonNull Expression expression` | The expression to count. |

| Returns |
|---|---|
| `@NonNull AggregateFunction` | A new `AggregateFunction` representing the 'count' aggregation. |

### count

```
public static final @NonNull AggregateFunction count(@NonNull String fieldName)
```

Creates an aggregation that counts the number of stage inputs where the input field exists.

| Parameters |
|---|---|
| `@NonNull String fieldName` | The name of the field to count. |

| Returns |
|---|---|
| `@NonNull AggregateFunction` | A new `AggregateFunction` representing the 'count' aggregation. |

### countAll

```
public static final @NonNull AggregateFunction countAll()
```

Creates an aggregation that counts the total number of stage inputs.

| Returns |
|---|---|
| `@NonNull AggregateFunction` | A new `AggregateFunction` representing the countAll aggregation. |

### countDistinct

```
public static final @NonNull AggregateFunction countDistinct(@NonNull Expression expression)
```

Creates an aggregation that counts the number of distinct values of an expression across multiple stage inputs.

| Parameters |
|---|---|
| `@NonNull Expression expression` | The expression to count the distinct values of. |

| Returns |
|---|---|
| `@NonNull AggregateFunction` | A new `AggregateFunction` representing the count distinct aggregation. |

### countDistinct

```
public static final @NonNull AggregateFunction countDistinct(@NonNull String fieldName)
```

Creates an aggregation that counts the number of distinct values of a field across multiple stage inputs.

| Parameters |
|---|---|
| `@NonNull String fieldName` | The name of the field to count the distinct values of. |

| Returns |
|---|---|
| `@NonNull AggregateFunction` | A new `AggregateFunction` representing the count distinct aggregation. |

### countIf

```
public static final @NonNull AggregateFunction countIf(@NonNull BooleanExpression condition)
```

Creates an aggregation that counts the number of stage inputs where the provided boolean expression evaluates to true.

| Parameters |
|---|---|
| `@NonNull BooleanExpression condition` | The boolean expression to evaluate on each input. |

| Returns |
|---|---|
| `@NonNull AggregateFunction` | A new `AggregateFunction` representing the count aggregation. |

### first

```
public static final @NonNull AggregateFunction first(@NonNull Expression expression)
```

Creates an aggregation that finds the first value of an expression across multiple stage inputs.

| Parameters |
|---|---|
| `@NonNull Expression expression` | The expression to find the first value of. |

| Returns |
|---|---|
| `@NonNull AggregateFunction` | A new `AggregateFunction` representing the first aggregation. |

### first

```
public static final @NonNull AggregateFunction first(@NonNull String fieldName)
```

Creates an aggregation that finds the first value of a field across multiple stage inputs.

| Parameters |
|---|---|
| `@NonNull String fieldName` | The name of the field to find the first value of. |

| Returns |
|---|---|
| `@NonNull AggregateFunction` | A new `AggregateFunction` representing the first aggregation. |

### last

```
public static final @NonNull AggregateFunction last(@NonNull Expression expression)
```

Creates an aggregation that finds the last value of an expression across multiple stage inputs.

| Parameters |
|---|---|
| `@NonNull Expression expression` | The expression to find the last value of. |

| Returns |
|---|---|
| `@NonNull AggregateFunction` | A new `AggregateFunction` representing the last aggregation. |

### last

```
public static final @NonNull AggregateFunction last(@NonNull String fieldName)
```

Creates an aggregation that finds the last value of a field across multiple stage inputs.

| Parameters |
|---|---|
| `@NonNull String fieldName` | The name of the field to find the last value of. |

| Returns |
|---|---|
| `@NonNull AggregateFunction` | A new `AggregateFunction` representing the last aggregation. |

### maximum

```
public static final @NonNull AggregateFunction maximum(@NonNull Expression expression)
```

Creates an aggregation that finds the maximum value of an expression across multiple stage inputs.

| Parameters |
|---|---|
| `@NonNull Expression expression` | The expression to find the maximum value of. |

| Returns |
|---|---|
| `@NonNull AggregateFunction` | A new `AggregateFunction` representing the maximum aggregation. |

### maximum

```
public static final @NonNull AggregateFunction maximum(@NonNull String fieldName)
```

Creates an aggregation that finds the maximum value of a field across multiple stage inputs.

| Parameters |
|---|---|
| `@NonNull String fieldName` | The name of the field to find the maximum value of. |

| Returns |
|---|---|
| `@NonNull AggregateFunction` | A new `AggregateFunction` representing the maximum aggregation. |

### minimum

```
public static final @NonNull AggregateFunction minimum(@NonNull Expression expression)
```

Creates an aggregation that finds the minimum value of an expression across multiple stage inputs.

| Parameters |
|---|---|
| `@NonNull Expression expression` | The expression to find the minimum value of. |

| Returns |
|---|---|
| `@NonNull AggregateFunction` | A new `AggregateFunction` representing the minimum aggregation. |

### minimum

```
public static final @NonNull AggregateFunction minimum(@NonNull String fieldName)
```

Creates an aggregation that finds the minimum value of a field across multiple stage inputs.

| Parameters |
|---|---|
| `@NonNull String fieldName` | The name of the field to find the minimum value of. |

| Returns |
|---|---|
| `@NonNull AggregateFunction` | A new `AggregateFunction` representing the minimum aggregation. |

### over

```
@Beta
public final @NonNull WindowFunction over(@NonNull WindowSpec window)
```

Applies a window frame to this aggregate, turning it into a window function.

Window Functions SDK features are in preview until the backend feature launches. Look for launch announcement at: https://docs.cloud.google.com/firestore/docs/release-notes

Use this to give a single accumulator its own framing, independent of the frame declared on the enclosing `addWindowFields` stage. Only `documents` or `range` window frames (created via `WindowSpec.documents` or `WindowSpec.range`) are supported on individual accumulators; other window parameters such as `WindowSpec.partition` or `WindowSpec.sort` are not supported on accumulator-level `over()` and must be specified on the enclosing `addWindowFields` stage.

| Parameters |
|---|---|
| `@NonNull WindowSpec window` | The window frame specification to evaluate this aggregate over. |

| Returns |
|---|---|
| `@NonNull WindowFunction` | A new `WindowFunction` wrapping this aggregate. |

### rawAggregate

```
public static final @NonNull AggregateFunction rawAggregate(@NonNull String name, @NonNull Expression expr)
```

Creates a raw aggregation function.

This method provides a way to call aggregation functions that are supported by the Firestore backend but that are not available as specific factory methods in this class.

| Parameters |
|---|---|
| `@NonNull String name` | The name of the aggregation function. |
| `@NonNull Expression expr` | The expressions to pass as arguments to the function. |

| Returns |
|---|---|
| `@NonNull AggregateFunction` | A new `AggregateFunction` for the specified function. |

### sum

```
public static final @NonNull AggregateFunction sum(@NonNull Expression expression)
```

Creates an aggregation that calculates the sum of values from an expression across multiple stage inputs.

| Parameters |
|---|---|
| `@NonNull Expression expression` | The expression to sum up. |

| Returns |
|---|---|
| `@NonNull AggregateFunction` | A new `AggregateFunction` representing the sum aggregation. |

### sum

```
public static final @NonNull AggregateFunction sum(@NonNull String fieldName)
```

Creates an aggregation that calculates the sum of a field's values across multiple stage inputs.

| Parameters |
|---|---|
| `@NonNull String fieldName` | The name of the field containing numeric values to sum up. |

| Returns |
|---|---|
| `@NonNull AggregateFunction` | A new `AggregateFunction` representing the sum aggregation. |