Aggregate functions - Amazon Timestream

# 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.

``SELECT arbitrary(t.c) FROM (VALUES 1, 2, 3, 4) AS t(c)``

Example result: `1`

array_agg(x)

array<[same as input]

Returns an array created from the input x elements.

``SELECT array_agg(t.c) FROM (VALUES 1, 2, 3, 4) AS t(c)``

Example result: `[ 1,2,3,4 ]`

avg(x)

double

Returns the average (arithmetic mean) of all input values.

``SELECT avg(t.c) FROM (VALUES 1, 2, 3, 4) AS t(c)``

Example result: `2.5`

bool_and(boolean) every(boolean)

boolean

Returns TRUE if every input value is TRUE, otherwise FALSE.

``SELECT bool_and(t.c) FROM (VALUES true, true, false, true) AS t(c)``

Example result: `false`

bool_or(boolean)

boolean

Returns TRUE if any input value is TRUE, otherwise FALSE.

``SELECT bool_or(t.c) FROM (VALUES true, true, false, true) AS t(c)``

Example result: `true`

count(*) count(x)

bigint

count(*) returns the number of input rows.

count(x) returns the number of non-null input values.

``SELECT count(t.c) FROM (VALUES true, true, false, true) AS t(c)``

Example result: `4`

count_if(x)

bigint

Returns the number of TRUE input values.

``SELECT count_if(t.c) FROM (VALUES true, true, false, true) AS t(c)``

Example result: `3`

geometric_mean(x)

double

Returns the geometric mean of all input values.

``SELECT geometric_mean(t.c) FROM (VALUES 1, 2, 3, 4) AS t(c)``

Example result: `2.213363839400643`

max_by(x, y)

[same as x]

Returns the value of x associated with the maximum value of y over all input values.

``SELECT max_by(t.c1, t.c2) FROM (VALUES (('a', 1)), (('b', 2)), (('c', 3)), (('d', 4))) AS t(c1, c2)``

Example result: `d`

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.

``SELECT max_by(t.c1, t.c2, 2) FROM (VALUES (('a', 1)), (('b', 2)), (('c', 3)), (('d', 4))) AS t(c1, c2)``

Example result: `[ d,c ]`

min_by(x, y)

[same as x]

Returns the value of x associated with the minimum value of y over all input values.

``SELECT min_by(t.c1, t.c2) FROM (VALUES (('a', 1)), (('b', 2)), (('c', 3)), (('d', 4))) AS t(c1, c2)``

Example result: `a`

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.

``SELECT min_by(t.c1, t.c2, 2) FROM (VALUES (('a', 1)), (('b', 2)), (('c', 3)), (('d', 4))) AS t(c1, c2)``

Example result: `[ a,b ]`

max(x)

[same as input]

Returns the maximum value of all input values.

``SELECT max(t.c) FROM (VALUES 1, 2, 3, 4) AS t(c)``

Example result: `4`

max(x, n)

array<[same as x]>

Returns n largest values of all input values of x.

``SELECT max(t.c, 2) FROM (VALUES 1, 2, 3, 4) AS t(c)``

Example result: `[ 4,3 ]`

min(x)

[same as input]

Returns the minimum value of all input values.

``SELECT min(t.c) FROM (VALUES 1, 2, 3, 4) AS t(c)``

Example result: `1`

min(x, n)

array<[same as x]>

Returns n smallest values of all input values of x.

``SELECT min(t.c, 2) FROM (VALUES 1, 2, 3, 4) AS t(c)``

Example result: `[ 1,2 ]`

sum(x)

[same as input]

Returns the sum of all input values.

``SELECT sum(t.c) FROM (VALUES 1, 2, 3, 4) AS t(c)``

Example result: `10`

bitwise_and_agg(x)

bigint

Returns the bitwise AND of all input values in 2’s complement representation.

``SELECT bitwise_and_agg(t.c) FROM (VALUES 1, -3) AS t(c)``

Example result: `1`

bitwise_or_agg(x)

bigint

Returns the bitwise OR of all input values in 2’s complement representation.

``SELECT bitwise_or_agg(t.c) FROM (VALUES 1, -3) AS t(c)``

Example result: `-3`

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.

``SELECT approx_distinct(t.c) FROM (VALUES 1, 2, 3, 4, 8) AS t(c)``

Example result: `5`

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].

``SELECT approx_distinct(t.c, 0.2) FROM (VALUES 1, 2, 3, 4, 8) AS t(c)``

Example result: `5`

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.

``SELECT approx_percentile(t.c, 0.4) FROM (VALUES 1, 2, 3, 4) AS t(c)``

Example result: `2`

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.

``SELECT approx_percentile(t.c, ARRAY[0.1, 0.8, 0.8]) FROM (VALUES 1, 2, 3, 4) AS t(c)``

Example result: `[ 1,4,4 ]`

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.

``SELECT approx_percentile(t.c, 1, 0.1) FROM (VALUES 1, 2, 3, 4) AS t(c)``

Example result: `1`

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.

``SELECT approx_percentile(t.c, 1, ARRAY[0.1, 0.8, 0.8]) FROM (VALUES 1, 2, 3, 4) AS t(c)``

Example result: `[ 1,4,4 ]`

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. The accuracy must be a value greater than zero and less than one, and it must be constant for all input rows.

``SELECT approx_percentile(t.c, 1, 0.1, 0.5) FROM (VALUES 1, 2, 3, 4) AS t(c)``

Example result: `1`

corr(y, x)

double

Returns correlation coefficient of input values.

``SELECT corr(t.c1, t.c2) FROM (VALUES ((1, 1)), ((2, 2)), ((3, 3)), ((4, 4))) AS t(c1, c2)``

Example result: `1.0`

covar_pop(y, x)

double

Returns the population covariance of input values.

``SELECT covar_pop(t.c1, t.c2) FROM (VALUES ((1, 1)), ((2, 2)), ((3, 3)), ((4, 4))) AS t(c1, c2)``

Example result: `1.25`

covar_samp(y, x)

double

Returns the sample covariance of input values.

``SELECT covar_samp(t.c1, t.c2) FROM (VALUES ((1, 1)), ((2, 2)), ((3, 3)), ((4, 4))) AS t(c1, c2)``

Example result: `1.6666666666666667`

regr_intercept(y, x)

double

Returns linear regression intercept of input values. y is the dependent value. x is the independent value.

``SELECT regr_intercept(t.c1, t.c2) FROM (VALUES ((1, 1)), ((2, 2)), ((3, 3)), ((4, 4))) AS t(c1, c2)``

Example result: `0.0`

regr_slope(y, x)

double

Returns linear regression slope of input values. y is the dependent value. x is the independent value.

``SELECT regr_slope(t.c1, t.c2) FROM (VALUES ((1, 1)), ((2, 2)), ((3, 3)), ((4, 4))) AS t(c1, c2)``

Example result: `1.0`

skewness(x)

double

Returns the skewness of all input values.

``SELECT skewness(t.c1) FROM (VALUES 1, 2, 3, 4, 8) AS t(c1)``

Example result: `0.8978957037987335`

stddev_pop(x)

double

Returns the population standard deviation of all input values.

``SELECT stddev_pop(t.c1) FROM (VALUES 1, 2, 3, 4, 8) AS t(c1)``

Example result: `2.4166091947189146`

stddev_samp(x) stddev(x)

double

Returns the sample standard deviation of all input values.

``SELECT stddev_samp(t.c1) FROM (VALUES 1, 2, 3, 4, 8) AS t(c1)``

Example result: `2.701851217221259`

var_pop(x)

double

Returns the population variance of all input values.

``SELECT var_pop(t.c1) FROM (VALUES 1, 2, 3, 4, 8) AS t(c1)``

Example result: `5.840000000000001`

var_samp(x) variance(x)

double

Returns the sample variance of all input values.

``SELECT var_samp(t.c1) FROM (VALUES 1, 2, 3, 4, 8) AS t(c1)``

Example result: `7.300000000000001`