# Aggregate Functions

Arroyo supports the following aggregate functions.
All of them except for count distinct are implemented in two phases,
which helps minimize the state requirements and scale out aggregations.
Nullity is handled in line with postgres:
`COUNT`

always returns the number of non-null entries,
all others ignore null arguments and return null if all arguments are null.

## Count

Count returns the number of non-null entries in the group.

The return type is always `BIGINT`

.

If `distinct`

is specified will be the number of unique non-null arguments.

```
-- number of records
count(*)
-- number of present ids
count(id)
-- number of distinct ids
count(distinct id)
```

## Sum

Returns the sum of numeric arguments.

32 bit numeric types will be promoted to 64 bit,
e.g. `INT`

to `BIGINT`

, `FLOAT`

to `DOUBLE`

.

```
sum(price * quantity)
```

## Avg

Returns the average of non-null arguments.

Result is always a `DOUBLE`

.

```
avg(price)
```

## Min

Returns the minimum of non-null arguments.

Result type is the same as the argument type.

```
min(price)
```

## Max

Returns the maximum of non-null arguments.

Result type is the same as the argument type.

```
max(price)
```