PostgreSQL: Aggregate Functions

I kind of skipped over the functions/operators section of the PostgreSQL documentation when reading it. I want to review the sections that I think would be useful to me.

Date Created:
1 17

References



Notes


Aggregate functions compute a single result from a set of unique values.

Function

Description

array_agg(expression)

input values, including nulls, concatenated into array

avg(expression)

the average (arithmetic mean) of all non-null input values

count(expression)

number of input rows for which the value of expression is not null

every(expression)

the bitwise AND of all non-null input values, or null if none

json_agg(expression)

aggregate values, including nulls, as a JSON array

json_object_agg(name,value)

aggregates name/value pairs as a JSON object; values can be null, but not names

max(expression)

maximum value of expression across all non-null input values

min(expression)

minimum value of expression across all non-null input values

string_agg(expression,delimeter)

non-null input values concatenated into a string, separated by delimiter

sum(expression)

sum of expression across all non-null input values

xmlagg(expression)

concatenation of non-null XML values

It should be noted that except for count, these functions return a null value when no rows are selected. The coalesce function can be used to substitute zero or an empty array for null when necessary.

The aggregate functions array_agg, json_agg, jsonb_agg, json_object_agg string_agg, and xml_agg, as well as similar user-defined functions, produce meaningfully different results depending on the order of the input values. This ordering is unspecified by default, but can be controlled by writing an ORDER BY clause within the aggregating call. Alternatively, supplying the input values from a sorted subquery will usually work.

SELECT xmlagg(x) FROM (SELECT x FROM test ORDER BY y DESC) AS tab;

This syntax is not allowed in the SQL standard, and is not portable to other database systems.

Comments

You have to be logged in to add a comment

User Comments

Insert Math Markup

ESC
About Inserting Math Content
Display Style:

Embed News Content

ESC
About Embedding News Content

Embed Youtube Video

ESC
Embedding Youtube Videos

Embed TikTok Video

ESC
Embedding TikTok Videos

Embed X Post

ESC
Embedding X Posts

Embed Instagram Post

ESC
Embedding Instagram Posts

Insert Details Element

ESC

Example Output:

Summary Title
You will be able to insert content here after confirming the title of the <details> element.

Insert Table

ESC
Customization
Align:
Preview:

Insert Horizontal Rule

#000000

Preview:


View Content At Different Sizes

ESC

Edit Style of Block Nodes

ESC

Edit the background color, default text color, margin, padding, and border of block nodes. Editable block nodes include paragraphs, headers, and lists.

#ffffff
#000000

Edit Selected Cells

Change the background color, vertical align, and borders of the cells in the current selection.

#ffffff
Vertical Align:
Border
#000000
Border Style:

Edit Table

ESC
Customization:
Align:

Upload Lexical State

ESC

Upload a .lexical file. If the file type matches the type of the current editor, then a preview will be shown below the file input.

Upload 3D Object

ESC

Upload Jupyter Notebook

ESC

Upload a Jupyter notebook and embed the resulting HTML in the text editor.

Insert Custom HTML

ESC

Edit Image Background Color

ESC
#ffffff

Insert Columns Layout

ESC
Column Type:

Select Code Language

ESC
Select Coding Language

Insert Chart

ESC

Use the search box below

Upload Previous Version of Article State

ESC