# Aggregate functions

Timestream supports the following aggregate functions.

Function | Output data type | Description |
---|---|---|

arbitrary(x) |
[same as input] |
Returns an arbitrary non-null value of x, if one exists. |

array_agg(x) |
array<[same as input] |
Returns an array created from the input x elements. |

avg(x) |
double |
Returns the average (arithmetic mean) of all input values. |

bool_and(boolean) every(boolean) |
boolean |
Returns TRUE if every input value is TRUE, otherwise FALSE. |

bool_or(boolean) |
boolean |
Returns TRUE if any input value is TRUE, otherwise FALSE. |

count(*) count(x) |
bigint |
count(*) returns the number of input rows. count(x) returns the number of non-null input values. |

count_if(x) |
bigint |
Returns the number of TRUE input values. |

geometric_mean(x) |
double |
Returns the geometric mean of all input values. |

max_by(x, y) |
[same as x] |
Returns the value of x associated with the maximum value of y over all input values. |

max_by(x, y, n) |
array<[same as x]> |
Returns n values of x associated with the n largest of all input values of y in descending order of y. |

min_by(x, y) |
[same as x] |
Returns the value of x associated with the minimum value of y over all input values. |

min_by(x, y, n) |
array<[same as x]> |
Returns n values of x associated with the n smallest of all input values of y in ascending order of y. |

max(x) |
[same as input] |
Returns the maximum value of all input values. |

max(x, n) |
array<[same as x]> |
Returns n largest values of all input values of x. |

min(x) |
[same as input] |
Returns the minimum value of all input values. |

min(x, n) |
array<[same as x]> |
Returns n smallest values of all input values of x. |

sum(x) |
[same as input] |
Returns the sum of all input values. |

bitwise_and_agg(x) |
bigint |
Returns the bitwise AND of all input values in 2’s complement representation. |

bitwise_or_agg(x) |
bigint |
Returns the bitwise OR of all input values in 2’s complement representation. |

approx_distinct(x) |
bigint |
Returns the approximate number of distinct input values. This function provides an approximation of count(DISTINCT x). Zero is returned if all input values are null. This function should produce a standard error of 2.3%, which is the standard deviation of the (approximately normal) error distribution over all possible sets. It does not guarantee an upper bound on the error for any specific input set. |

approx_distinct(x, e) |
bigint |
Returns the approximate number of distinct input values. This function provides an approximation of count(DISTINCT x). Zero is returned if all input values are null. This function should produce a standard error of no more than e, which is the standard deviation of the (approximately normal) error distribution over all possible sets. It does not guarantee an upper bound on the error for any specific input set. The current implementation of this function requires that e be in the range of [0.0040625, 0.26000]. |

approx_percentile(x, percentage) |
[same as x] |
Returns the approximate percentile for all input values of x at the given percentage. The value of percentage must be between zero and one and must be constant for all input rows. |

approx_percentile(x, percentages) |
array<[same as x]> |
Returns the approximate percentile for all input values of x at each of the specified percentages. Each element of the percentages array must be between zero and one, and the array must be constant for all input rows. |

approx_percentile(x, w, percentage) |
[same as x] |
Returns the approximate weighed percentile for all input values of x using the per-item weight w at the percentage p. The weight must be an integer value of at least one. It is effectively a replication count for the value x in the percentile set. The value of p must be between zero and one and must be constant for all input rows. |

approx_percentile(x, w, percentages) |
array<[same as x]> |
Returns the approximate weighed percentile for all input values of x using the per-item weight w at each of the given percentages specified in the array. The weight must be an integer value of at least one. It is effectively a replication count for the value x in the percentile set. Each element of the array must be between zero and one, and the array must be constant for all input rows. |

approx_percentile(x, w, percentage, accuracy) |
[same as x] |
Returns the approximate weighed percentile for all input values of x using the per-item weight w at the percentage p, with a maximum rank error of accuracy. The weight must be an integer value of at least one. It is effectively a replication count for the value x in the percentile set. The value of p must be between zero and one and must be constant for all input rows. accuracy must be a value greater than zero and less than one, and it must be constant for all input rows. |

corr(y, x) |
double |
Returns correlation coefficient of input values. |

covar_pop(y, x) |
double |
Returns the population covariance of input values. |

covar_samp(y, x) |
double |
Returns the sample covariance of input values. |

regr_intercept(y, x) |
double |
Returns linear regression intercept of input values. y is the dependent value. x is the independent value. |

regr_slope(y, x) |
double |
Returns linear regression slope of input values. y is the dependent value. x is the independent value. |

skewness(x) |
double |
Returns the skewness of all input values. |

stddev_pop(x) |
double |
Returns the population standard deviation of all input values. |

stddev_samp(x) stddev(x) |
double |
Returns the sample standard deviation of all input values. |

var_pop(x) |
double |
Returns the population variance of all input values. |

var_samp(x) variance(x) |
double |
Returns the sample variance of all input values. |