From 4781445223f33ca00713e3768d8e9d052596a09d Mon Sep 17 00:00:00 2001 From: "David G. Johnston" Date: Wed, 14 Dec 2022 02:28:49 +0000 Subject: [PATCH] Document aggregate order by influenced functions --- doc/src/sgml/func.sgml | 90 +++++++++++++++++++++++++++++++++++------- 1 file changed, 76 insertions(+), 14 deletions(-) diff --git a/doc/src/sgml/func.sgml b/doc/src/sgml/func.sgml index ad31fdb737..9f6cfd6d42 100644 --- a/doc/src/sgml/func.sgml +++ b/doc/src/sgml/func.sgml @@ -19693,6 +19693,23 @@ SELECT NULLIF(value, '(none)') ... aggregation. + + + The aggregates in this section all accept an optional + ORDER BY clause as described in the + general syntax rules for aggregates (see ). + On this page only those aggregates whose output will vary depending + upon the ordering of the inputs are shown with the clause. + It may be omitted, though the syntax here does not reflect this, + except in the case of float input aggregates. + + + input_sort_expr - An expression composed from the input + columns used to sort the input rows before providing them to the aggregate. + Typically one or more input column names separated by commas. + + + General-Purpose Aggregate Functions @@ -19716,24 +19733,43 @@ SELECT NULLIF(value, '(none)') ... array_agg - array_agg ( anynonarray ) + array_agg ( anynonarray ORDER BY input_sort_expr) anyarray Collects all the input values, including nulls, into an array. - + + + WITH vals (v) AS ( VALUES (1),(3),(4),(3),(2) ) + + + SELECT array_agg(v ORDER BY v) FROM vals + {1,2,3,3,4} + + + SELECT array_agg(DISTINCT v) FROM vals + {1,2,3,4} + + No - array_agg ( anyarray ) + array_agg ( anyarray ORDER BY input_sort_expr ) anyarray Concatenates all the input arrays into an array of one higher dimension. (The inputs must all have the same dimensionality, and cannot be empty or null.) + + + WITH vals (v) AS ( VALUES (ARRAY[1,3]::integer[]), (ARRAY[3,2]), (ARRAY[1,4]) ) + + + SELECT array_agg(v ORDER BY v) FROM vals + {{1,3},{1,4},{3,2}} No @@ -19762,11 +19798,11 @@ SELECT NULLIF(value, '(none)') ... numeric - avg ( real ) + avg ( real ORDER BY input_sort_expr ) double precision - avg ( double precision ) + avg ( double precision ORDER BY input_sort_expr ) double precision @@ -19932,14 +19968,14 @@ SELECT NULLIF(value, '(none)') ... json_agg - json_agg ( anyelement ) + json_agg ( anyelement ORDER BY input_sort_expr ) json jsonb_agg - jsonb_agg ( anyelement ) + jsonb_agg ( anyelement ORDER BY input_sort_expr ) jsonb @@ -19957,7 +19993,8 @@ SELECT NULLIF(value, '(none)') ... json_object_agg ( key "any", value - "any" ) + "any" + ORDER BY input_sort_expr ) json @@ -19966,7 +20003,8 @@ SELECT NULLIF(value, '(none)') ... jsonb_object_agg ( key "any", value - "any" ) + "any" + ORDER BY input_sort_expr ) jsonb @@ -19974,7 +20012,23 @@ SELECT NULLIF(value, '(none)') ... are coerced to text; value arguments are converted as per to_json or to_jsonb. Values can be null, but not keys. - + + + The keys of a jsonb document are definitionally unordered and unique, + the last value seen is kept. + + + WITH vals (k, v) AS ( VALUES ('key1','1'),('key0','2'),('key1','3' ) + + + SELECT json_object_agg(k, v ORDER BY v) FROM vals + { "key1" : "1", "key0" : "2", "key1" : "3" } + + + SELECT jsonb_object_agg(k, v ORDER BY v) FROM vals + {"key0": "2", "key1": "3"} + + No @@ -20067,13 +20121,21 @@ SELECT NULLIF(value, '(none)') ... string_agg ( value - bytea, delimiter bytea ) + bytea, delimiter bytea + ORDER BY input_sort_expr ) bytea Concatenates the non-null input values into a string. Each value after the first is preceded by the corresponding delimiter (if it's not null). + + + WITH vals (v) AS ( VALUES ('Larry', 'Curley', 'Moe'::text) ) + + + SELECT array_agg(v, ', ' ORDER BY v) FROM vals + Curley, Larry, Moe No @@ -20099,11 +20161,11 @@ SELECT NULLIF(value, '(none)') ... numeric - sum ( real ) + sum ( real ORDER BY input_sort_expr ) real - sum ( double precision ) + sum ( double precision ORDER BY input_sort_expr ) double precision @@ -20125,7 +20187,7 @@ SELECT NULLIF(value, '(none)') ... xmlagg - xmlagg ( xml ) + xmlagg ( xml ORDER BY input_sort_expr ) xml -- 2.25.1