AnalyticDB for PostgreSQL is compatible with the aggregate functions of PostgreSQL. This topic describes the aggregate functions that are supported by AnalyticDB for PostgreSQL and provides examples on how to use the aggregate functions.
For more information, see Aggregate Functions.
Aggregate functions
Overview
Aggregate functions perform calculations on a set of data and return corresponding results. You can use aggregate functions in databases to calculate the results of multiple input rows for statistics, such as the average value, maximum value, and sum. If no input rows exist, all functions except the count() and count(*) functions return a null value instead of zero. If no input rows exist, the array_agg() function returns null instead of an empty array. If necessary, you can use the coalesce() function to replace zeros or empty arrays with null.
General-purpose aggregate functions
Function | Data type of the input values | Data type of the return value | Description |
| Any non-array type (We recommend that you do not temporarily use types wrapped by using the row() function as the data type of the input values.) | Same as the data type of the input values | Concatenates input values, including null values, into an array. |
| Any array type | Same as the data type of the input values | Concatenates input arrays into an array of one higher dimension. The input values must have the same dimensionality and cannot be empty or null. |
|
|
| Returns the average value of all non-null input values. |
|
| Same as the data type of the input values | Returns the bitwise AND of all non-null input values. If no non-null input values exist, this function returns null. |
|
| Same as the data type of the input values | Returns the bitwise OR of all non-null input values. If no non-null input values exist, this function returns null. |
|
|
| Returns true if all input values are true. Otherwise, this function returns false. |
|
|
| Returns true if at least one input value is true. Otherwise, this function returns false. |
| - |
| Returns the number of input rows. |
|
|
| Returns the number of non-null input rows. |
|
|
| Works in the same manner as the |
|
|
| Aggregates values, including null values, into a JSON array. |
|
|
| Aggregates values, including null values, into a JSONB array. |
|
|
| Aggregates |
|
|
| Aggregates |
| Any array, numeric, string, date or time, network, or enumeration type, or array of these types | Same as the data type of the input values | Returns the maximum value of the non-null input values. |
| Any array, numeric, string, date or time, network, or enumeration type, or array of these types | Same as the data type of the input values | Returns the minimum value of the non-null input values. |
|
| Same as the data type of the input values | Concatenates non-null input values into a string that is separated by the specified delimiter. |
|
|
| Returns the sum of all non-null input values. |
|
|
| Concatenates non-null XML input values. |
Statistics-related aggregate functions
Function | Data type of the input values | Data type of the return value | Description |
|
|
| Returns the correlation coefficient between variables Y and X. |
|
|
| Returns the population covariance between variables Y and X. |
|
|
| Returns the sample covariance between variables Y and X. |
|
|
| Returns the average value of the independent variable X by using the following formula: |
|
|
| Returns the average value of the dependent variable Y by using the following formula: |
|
|
| Returns the number of input rows in which the values for both variables Y and X are non-null values. |
|
|
| Returns the y-intercept of the linear equation fitted by least squares from the data points of variables |
|
|
| Returns the square of the correlation coefficient. |
|
|
| Returns the slope of the linear equation fitted by least squares from the data points of variables |
|
|
| Returns the sum of the squares of the independent variable X by using the following formula: |
|
|
| Returns the sum of the products of the independent variable X and the dependent variable Y by using the following formula: |
|
|
| Returns the sum of the squares of the dependent variable Y by using the following formula: |
|
|
| Works in the same manner as the |
|
|
| Returns the population standard deviation of the input values. |
|
|
| Returns the sample standard deviation of the input values. |
|
|
| Works in the same manner as the |
|
|
| Returns the population variance of the input values, which is the square of the population standard deviation. |
|
|
| Returns the sample variance of the input values, which is the square of the sample standard deviation. |