General-purpose aggregate functions
approx_count_distinct
Added in v2.5.0.
This function is only supported for append-only streams, and it is currently in technical preview stage.
array_agg
Collects all the input values, including nulls, into an array. The ORDER BY
clause is optional and specifies the order of rows processed in the aggregation, which determines the order of the elements in the result array.
avg
Returns the average (arithmetic mean) of all non-null input values or null if no non-null values are provided.
bit_and
Returns the bitwise AND of all non-null input values or null if no non-null values are provided.
bit_or
Returns the bitwise OR of all non-null input values or null if no non-null values are provided.
bool_and
Returns true if all non-null input values are true, otherwise false.
bool_or
Returns true if any non-null input value is true, otherwise false.
count
Returns the number of non-null input values.
count(*)
Returns the number of rows in the input.
jsonb_agg
Collects all the input values, including nulls, into a JSON array. The ORDER BY
clause is optional and specifies the order of rows processed in the aggregation, which determines the order of the elements in the result array.
jsonb_object_agg
Aggregates name/value pairs as a JSON object. Values can be null, but keys cannot.
max
Returns the maximum of the non-null input values, or null if no non-null values are provided.
min
Returns the minimum value of the non-null input values, or null if no non-null values are provided.
string_agg
Concatenates non-null input values into a string. Each value after the first is preceded by the corresponding delimiter (if it’s not null). If no non-null values are provided, returns null. The ORDER BY
clause is optional and specifies the order of rows processed in the aggregation, which determines the order of the elements in the result array.
sum
Returns the sum of all non-null input values, or null if no non-null values are provided.
first_value
Returns the first value in an ordered set of values, including nulls.
order_key
is the column or expression used to determine the order of the values. It is required to make the result deterministic.
last_value
Returns the last value in an ordered set of values, including nulls.
Aggregate functions for statistics
stddev_pop
Calculates the population standard deviation of the input values. Returns NULL
if the input contains no non-null values.
stddev_samp
Calculates the sample standard deviation of the input values. Returns NULL
if the input contains fewer than two non-null values.
var_pop
Calculates the population variance of the input values. Returns NULL
if the input contains no non-null values.
var_samp
Calculates the sample variance of the input values. Returns NULL
if the input contains fewer than two non-null values.
Ordered-set aggregate functions
At present, ordered-set aggregate functions support only constant fraction arguments.
mode
Computes the mode, which is the most frequent value of the aggregated argument. If there are multiple equally-frequent values, it arbitrarily chooses the first one.
sort_expression
: Must be of a sortable type.
This example calculates the mode of the values in column1
from table1
.
percentile_cont
At present,
percentile_cont
is not supported for streaming queries yet.fraction
: The fraction value representing the desired percentile. It should be between 0 and 1.
This example calculates the median (50th percentile) of the values in column1
from table1
.
percentile_disc
At present,
percentile_disc
is not supported for streaming queries yet.fraction
: The fraction value representing the desired percentile. It should be between 0 and 1.
sort_expression
: Must be of a sortable type.
This example calculates the 75th percentile of the values in column1
from table1
.
approx_percentile
Returns an approximate value of the specified percentile from a numeric column.
percentile
: The percentile to approximate. It should be between 0 and 1.relative_error
: Optional. Specifies the maximum allowed error in the approximation. Defaults to 0.01 (1%).percentile_column
: The column from which to calculate the percentile. Must be of a numeric type.
Grouping operation functions
Grouping operation functions are used in conjunction with grouping sets to distinguish result rows. The arguments to thegrouping()
function are not actually evaluated, but they must exactly match expressions given in the GROUP BY
clause of the associated query level.
grouping
Returns a bit mask indicating which GROUP BY
expressions are not included in the current grouping set. Bits are assigned with the rightmost argument corresponding to the least-significant bit; each bit is 0 if the corresponding expression is included in the grouping criteria of the grouping set generating the current result row, and 1 if it is not included.
Syntax
Example
Create a table
Insert some data
Get grouping results