Menu

PostgreSQL Aggregate Functions

PostgreSQL aggregate functions are mainly used for data statistics, such as count, sum, average, etc. Aggregate functions combined with grouping operations allow you to count data in more dimensions. For JSON array and object aggregates and their cross-database differences, see SQL JSON aggregation by database.

  1. array_agg

    PostgreSQL array_agg() collects input values into an array, including NULL values. Add ORDER BY inside the aggregate to control element order.
  2. avg

    The PostgreSQL avg() function is an aggregate function that computes the average of all specified values ​​in a group and returns it.
  3. bit_and

    The PostgreSQL bit_and() function is an aggregate function that performs a “bitwise AND” operation on all non-null input values.
  4. bit_or

    The PostgreSQL bit_or() function is an aggregate function that performs a “bitwise OR” of all non-null input values.
  5. bit_xor

    The PostgreSQL bit_xor() function is an aggregate function that performs a “bitwise XOR” operation on all non-null input values.
  6. bool_and

    The PostgreSQL bool_and() function is an aggregate function that returns the “logical AND” of all specified non-null Boolean values ​​in a group.
  7. bool_or

    The PostgreSQL bool_or() function is an aggregate function that returns the “logical OR” of all specified non-null Boolean values ​​in a group.
  8. count

    The PostgreSQL count() function is an aggregate function that counts and returns the number of all specified values ​​in a group.
  9. every

    The PostgreSQL every() function is an aggregate function that returns the result of the “logical AND” of all specified non-null Boolean values ​​in a group.
  10. json_agg

    PostgreSQL json_agg() collects input values, including NULL, into a JSON array. Use ORDER BY inside the aggregate to control element order.
  11. json_object_agg

    PostgreSQL json_object_agg() builds a json object from key-value rows. Keys cannot be NULL; values may be NULL and become JSON null.
  12. jsonb_agg

    PostgreSQL jsonb_agg() collects input values, including NULL, into a JSONB array. Use ORDER BY inside the aggregate to control element order.
  13. jsonb_object_agg

    PostgreSQL jsonb_object_agg() returns jsonb from key-value rows. Keys cannot be NULL; values may be NULL, and duplicate-key behavior depends on input order.
  14. max

    The PostgreSQL max() function is an aggregate function that returns the maximum value of all specified values ​​in a group.
  15. min

    The PostgreSQL min() function is an aggregate function that returns the minimum value of all specified values ​​in a group.
  16. range_agg

    The PostgreSQL range_agg() function is an aggregate function that returns the union of all non-NULL input range values ​​in a group.
  17. range_intersect_agg

    The PostgreSQL range_intersect_agg() function is an aggregate function that returns the intersection of all non-NULL input range values ​​in a group.
  18. stddev

    The PostgreSQL stddev() function is an aggregate function that computes the sample standard deviation of all non-null input values.
  19. stddev_pop

    The PostgreSQL stddev_pop() function is an aggregate function that computes the population standard deviation of all non-null input values.
  20. stddev_samp

    The PostgreSQL stddev_samp() function is an aggregate function that computes the sample standard deviation of all non-null input values.
  21. string_agg

    PostgreSQL string_agg() concatenates non-NULL text values with a delimiter. Use ORDER BY inside the aggregate to control the output order.
  22. sum

    The PostgreSQL sum() function is an aggregate function that computes the sum of all specified values ​​in a group and returns the result.
  23. var_pop

    The PostgreSQL var_pop() function is an aggregate function that computes the population variance (square of the population standard deviation) for all non-null input values.
  24. var_samp

    The PostgreSQL var_samp() function is an aggregate function that computes the sample variance (square of the sample standard deviation) for all non-null input values.
  25. variance

    The PostgreSQL variance() function is an aggregate function that computes the sample variance (square of the sample standard deviation) for all non-null input values.