Aggregate Functions
Aggregate functions are commonly used functions in databases for aggregating calculations and summaries.They take a set of values as input and return a single aggregated result.Aggregate functions can be used to perform various operations, such as calculating totals, averages, maximums, minimums, etc.
General
avg
Returns the average of the values in the specified column.
avg(expression)
Parameters | Description |
---|---|
expression | Expression to operate on.Can be a constant, column, or function, and any combination of arithmetic operators. |
View avg
Example
count
Returns the number of rows in the specified column.
count(expression)
Parameters | Description |
---|---|
expression | Expression to operate on.Can be a constant, column, or function, and any combination of arithmetic operators. |
View count
Example
max
Returns the number of rows in the specified column.
max(expression)
Parameters | Description |
---|---|
expression | Expression to operate on.Can be a constant, column, or function, and any combination of arithmetic operators. |
View max
Example
mean
Alias for avg
.
median
返回指定列中的中值。
median(expression)
Parameters | Description |
---|---|
expression | Expression to operate on.Can be a constant, column, or function, and any combination of arithmetic operators. |
View median
Example
min
Returns the minimum value in the specified column.
min(expression)
Parameters | Description |
---|---|
expression | Expression to operate on.Can be a constant, column, or function, and any combination of arithmetic operators. |
View min
Example
sum
Returns the sum of all values in the specified column.
sum(expression)
Parameters | Description |
---|---|
expression | Expression to operate on.Can be a constant, column, or function, and any combination of arithmetic operators. |
View sum
Example
array_agg
Returns an array created from expression elements.If sorting requirements are given, insert elements in the order required for sorting.
array_agg(expression [ORDER BY expression])
Parameters | Description |
---|---|
expression | Expression to operate on.Can be a constant, column, or function, and any combination of arithmetic operators. |
View array_agg
Example
first_value
Return the first element in the aggregate group in the order of the request.If no order is specified, returns an arbitrary element from the group.
first_value(expression [ORDER BY expression])
Parameters | Description |
---|---|
expression | Expression to operate on.Can be a constant, column, or function, and any combination of arithmetic operators. |
View first_value
Example
last_value
Return the first element in the aggregate group in the order of the request.If no order is specified, returns an arbitrary element from the group.
last_value(expression [ORDER BY expression])
Parameters | Description |
---|---|
expression | Expression to operate on.Can be a constant, column, or function, and any combination of arithmetic operators. |
View last_value
Example
increase
Calculate the increment of the time series in the range vector, similar to the increase function in Prometheus.
increase(time_expression, expression ORDER BY time_expression)
Parameters | Description |
---|---|
time_expression | Must be for the time column. |
expression | Expression to operate on.Can be a constant, column, or function, and any combination of arithmetic operators. |
View mode
Example
Statistics
corr
Calculate the Pearson correlation coefficient between two columns, which is a method of measuring the degree of linear correlation between two variables.The value of Pearson correlation coefficient ranges between -1 and 1, where 1 indicates perfect positive correlation, -1 indicates perfect negative correlation, and 0 indicates no linear correlation.
corr(expression1, expression2)
Parameters | Description |
---|---|
expression1 | The first expression to operate.Can be a constant, column, or function, and any combination of arithmetic operators. |
expression1 | The second expression to operate.Can be a constant, column, or function, and any combination of arithmetic operators. |
View corr
Example
covar
Return a set of covariance of number pairs.
covar(expression1, expression2)
Parameters | Description |
---|---|
expression1 | The first expression to operate.Can be a constant, column, or function, and any combination of arithmetic operators. |
expression1 | The second expression to operate.Can be a constant, column, or function, and any combination of arithmetic operators. |
View covar
Example
covar_pop
Return the total covariance of a set of number pairs.
covar_pop(expression1, expression2)
Parameters | Description |
---|---|
expression1 | The first expression to operate.Can be a constant, column, or function, and any combination of arithmetic operators. |
expression1 | The second expression to operate.Can be a constant, column, or function, and any combination of arithmetic operators. |
View covar_pop
Example
covar_samp
Return a set of sample covariance of number pairs.
covar_samp(expression1, expression2)
Parameters | Description |
---|---|
expression1 | The first expression to operate.Can be a constant, column, or function, and any combination of arithmetic operators. |
expression1 | The second expression to operate.Can be a constant, column, or function, and any combination of arithmetic operators. |
View covar_samp
Example
stddev
Returns the standard deviation of a set of numbers.
stddev(expression)
Parameters | Description |
---|---|
expression | Expression to operate on.Can be a constant, column, or function, and any combination of arithmetic operators. |
View stddev
Example
stddev_pop
Returns the overall standard deviation of a set of numbers.
stddev_pop(expression)
Parameters | Description |
---|---|
expression | Expression to operate on.Can be a constant, column, or function, and any combination of arithmetic operators. |
View stddev_pop
Example
stddev_samp
Returns the sample standard deviation of a set of numbers.
stddev_samp(expression)
Parameters | Description |
---|---|
expression | Expression to operate on.Can be a constant, column, or function, and any combination of arithmetic operators. |
View stddev_samp
Example
var
Return a set of statistical variances for a set of numbers.
var(expression)
Parameters | Description |
---|---|
expression | Expression to operate on.Can be a constant, column, or function, and any combination of arithmetic operators. |
View var
Example
var_pop
Return the total population variance of a set of numbers.
var_pop(expression)
Parameters | Description |
---|---|
expression | Expression to operate on.Can be a constant, column, or function, and any combination of arithmetic operators. |
View var_pop
Example
var_samp
Returns the statistical sample variance of a set of numbers.
var_samp(expression)
Parameters | Description |
---|---|
expression | Expression to operate on.Can be a constant, column, or function, and any combination of arithmetic operators. |
View var_samp
Example
Approx
approx_distinct
Returns the approximate number of distinct input values calculated using the HyperLogLog algorithm.
approx_distinct(expression)
Parameters | Description |
---|---|
expression | Expression to operate on.Can be a constant, column, or function, and any combination of arithmetic operators. |
View asap_smooth
Example
approx_median
Returns the approximate median of the input values (the 50th percentile).It is an alias for approx_percentile_cont(x, 0.5)
.
approx_median(expression)
Parameters | Description |
---|---|
expression | Expression to operate on.Can be a constant, column, or function, and any combination of arithmetic operators. |
View approx_median
Example
approx_percentile_cont
Returns the weighted approximate percentile of the input values using the t-digest algorithm.
approx_percentile_cont(expression, percentile, centroids)
Parameters | Description |
---|---|
expression | Expression to operate on.Can be a constant, column, or function, and any combination of arithmetic operators. |
percentile | The percentile to be calculated.Must be a floating point value between 0 and 1 (inclusive). |
centroids | Optional, the centroid number used in the t-digest algorithm.Default value is 100. |
If there are this number or fewer unique values, exact results can be expected.The more centroids there are, the more accurate the approximate value, but more memory is needed for calculation.
View approx_percentile_cont
Example
approx_percentile_cont_with_weight
Returns the weighted approximate percentile of the input values using the t-digest algorithm.
approx_percentile_cont_with_weight(expression, weight, percentile)
Parameters | Description |
---|---|
expression | Expression to operate on.Can be a constant, column, or function, and any combination of arithmetic operators. |
weight | Expression used as a weight.Can be a constant, column, or function, and any combination of arithmetic operators. |
percentile | The percentile to be calculated.Must be a floating point value between 0 and 1 (inclusive). |
If there are this number or fewer unique values, exact results can be expected.The more centroids there are, the more accurate the approximate value, but more memory is needed for calculation.
View approx_percentile_cont_with_weight
Example
sample
Select n random records from the given column.
sample(expression, n)
Parameters | Description |
---|---|
expression | Expression to operate on.Must be a column in the table. |
n | Number of records to be returned. |
View sample
Example
- 社区版
- 企业版
asap_smooth
The asap_smooth
function is used for smoothing time series data. The ASAP (As Smooth As Possible) smoothing algorithm aims to quickly smooth time series data while preserving key trends and patterns in the data, making the visualization of the data clearer without losing important information due to excessive smoothing.
asap_smooth(time, value, resolution ORDER BY time)
Parameters | Description |
---|---|
time | Timestamp of each data point. |
value | The value of each timestamp. |
resolution | Approximate points to return.Determine the horizontal resolution of the resulting image. |