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.
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_aggstring_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 ORDERBY y DESC)AS tab;
This syntax is not allowed in the SQL standard, and is not portable to other database systems.
Are you sure you want to delete this article section? You can not undo this change.
Add a Comment
Annotate Article
Share Article
Successfully copied article URL to clipboard!
Something went wrong copying the article URL to the clipboard.
Successfully copied editor state to clipboard!
A previous, saved version of this article is saved. To compare the current version of the article with the previous version - or to replace the current version with the saved version - click the icon above the text editor.
Successfully copied URL to clipboard!
Copied code to clipboard.
Copied TeX code to clipboard.
Uploaded file must be an image of type .jpeg, .jpg, .png, .webp, .gif, .bmp, or .svg.
Image Size must be less than 5MB.
Uploaded file must be an image of type jpeg, jpg, png, webp, avif, tiff, or svg.
Something went wrong uploading the image to the database. Try reloading the page.
The maximum number of images you can upload is 30.
There was an error taking an image using the device's camera. Try uploading an image instead.
Uploaded file must be an audio file.
Audio file must be an audio file of type .m4a, .flac, .mp3, .mp4, .wav, .wma, .aac, .webm, or .mpeg.
Audio file must be less than 300 MB in size.
Something went wrong uploading the audio file. Try reloading the page.
The maximum number of audio files you can upload is 10.
There was an error capturing an audio recording using the device's microphone. Try uploading an audio recording or video instead.
Uploaded file must be an video file.
Video file must be an audio file of type .mp4, .mov, .avi, .wmv, .avchd, .webm, or .flv.
Video file must be less than 300 MB in size.
Something went wrong uploading the video file. Try reloading the page.
The maximum number of video files you can upload is 10.
There was an error taking a video using the device's camera / microphone. Try uploading a video instead.
Comments
You have to be logged in to add a comment
User Comments
There are currently no comments for this article.