| title | sidebar_label | description |
|---|---|---|
| Aggregate functions | Aggregate | Aggregate functions reference documentation. |
This page describes the available functions to assist with performing aggregate calculations.
avg
avg(value) calculates simple average of values ignoring missing data (e.g null values).
Arguments:
valueis any numeric value.
Return value:
Return value type is double.
Examples:
1. SELECT avg(amount) FROM transactions;
| avg |
|---|
| 22.4 |
1. SELECT payment_type, avg(amount) FROM transactions;
| cash_or_card | avg |
|---|---|
| cash | 22.1 |
| card | 27.4 |
| null | 18.02 |
count
count() or count(*) - counts rows irrespective of underlying data.
Arguments:
countdoes not require arguments.
Return value:
Return value type is long.
Examples:
- Count of rows in the transactions table.
1. SELECT count() FROM transactions;
| count |
|---|
| 100 |
- Count of rows in the transactions table aggregated by
payment_typevalue.
1. SELECT payment_type, count() FROM transactions;
| cash_or_card | count |
|---|---|
| cash | 25 |
| card | 70 |
| null | 5 |
:::note
null values are aggregated with count().
:::
count_distinct
count_distinct(STRING_COL) or count_distinct(SYMBOL_COL) - counts distinct values in STRING or SYMBOL columns.
Return value:
Return value type is long.
Examples:
- Count of distinct sides in the transactions table. Side column can either be
BUYorSELLornull
1. SELECT count_distinct(side) FROM transactions;
| count_distinct |
|---|
| 2 |
- Count of distinct counterparties in the transactions table aggregated by
payment_typevalue.
1. SELECT payment_type, count_distinct(counterparty) FROM transactions;
| cash_or_card | count_distinct |
|---|---|
| cash | 3 |
| card | 23 |
| null | 5 |
:::note
null values are not counted in `count\_distinct functions.
:::
haversine_dist_deg
haversine_dist_deg(lat, lon, ts) - calculates the traveled distance for a series of latitude and longitude points.
Arguments:
latis the latitude expressed as degrees in decimal format (double)lonis the longitude expressed as degrees in decimal format (double)tsis thetimestampfor the data point
Return value:
Return value type is double.
Examples:
1. SELECT car_id, haversine_dist_deg(lat, lon, k)
2. FROM table rides
ksum
ksum(value) - adds values ignoring missing data (e.g null values). Values are added using the
Kahan compensated sum algorithm. This is only beneficial for floating-point values such as float or double.
Arguments:
valueis any numeric value.
Return value:
Return value type is the same as the type of the argument.
Examples:
1. SELECT ksum(a)
2. FROM (SELECT rnd_double() a FROM long_sequence(100));
| ksum |
|---|
| 52.79143968514029 |
max
max(value) - returns the highest value ignoring missing data (e.g null values).
Arguments:
valueis any numeric value
Return value:
Return value type is the same as the type of the argument.
Examples:
1. SELECT max(amount) FROM transactions;
| min |
|---|
| 55.3 |
1. SELECT payment_type, max(amount) FROM transactions;
| cash_or_card | amount |
|---|---|
| cash | 31.5 |
| card | 55.3 |
| null | 29.2 |
min
min(value) - returns the lowest value ignoring missing data (e.g null values).
Arguments:
valueis any numeric value
Return value:
Return value type is the same as the type of the argument.
Examples:
1. SELECT min(amount) FROM transactions;
| min |
|---|
| 12.5 |
1. SELECT payment_type, min(amount) FROM transactions;
| cash_or_card | min |
|---|---|
| cash | 12.5 |
| card | 15.3 |
| null | 22.2 |
nsum
nsum(value) - adds values ignoring missing data (e.g null values). Values are added using the Neumaier sum algorithm. This is only beneficial for floating-point values such as float or double.
Arguments:
valueis any numeric value.
Return value:
Return value type is the same as the type of the argument.
Examples:
1. SELECT nsum(a)
2. FROM (SELECT rnd_double() a FROM long_sequence(100));
| nsum |
|---|
| 49.5442334742831 |
sum
sum(value) - adds values ignoring missing data (e.g null values).
Arguments:
valueis any numeric value.
Return value:
Return value type is the same as the type of the argument.
Examples:
1. SELECT sum(quantity) FROM transactions;
| sum |
|---|
| 100 |
1. SELECT item, sum(quantity) FROM transactions;
| item | count |
|---|---|
| apple | 53 |
| orange | 47 |
Overflow
sum does not perform overflow check. To avoid overflow, you can cast the argument to wider type.
1. SELECT sum(cast(a AS LONG)) FROM table;
