]> begriffs open source - ai-pg/blob - full-docs/txt/functions-aggregate.txt
Convert HTML docs to more streamlined TXT
[ai-pg] / full-docs / txt / functions-aggregate.txt
1
2 9.21. Aggregate Functions #
3
4    Aggregate functions compute a single result from a set of input values.
5    The built-in general-purpose aggregate functions are listed in
6    Table 9.62 while statistical aggregates are in Table 9.63. The built-in
7    within-group ordered-set aggregate functions are listed in Table 9.64
8    while the built-in within-group hypothetical-set ones are in
9    Table 9.65. Grouping operations, which are closely related to aggregate
10    functions, are listed in Table 9.66. The special syntax considerations
11    for aggregate functions are explained in Section 4.2.7. Consult
12    Section 2.7 for additional introductory information.
13
14    Aggregate functions that support Partial Mode are eligible to
15    participate in various optimizations, such as parallel aggregation.
16
17    While all aggregates below accept an optional ORDER BY clause (as
18    outlined in Section 4.2.7), the clause has only been added to
19    aggregates whose output is affected by ordering.
20
21    Table 9.62. General-Purpose Aggregate Functions
22
23    Function
24
25    Description
26    Partial Mode
27
28    any_value ( anyelement ) → same as input type
29
30    Returns an arbitrary value from the non-null input values.
31    Yes
32
33    array_agg ( anynonarray ORDER BY input_sort_columns ) → anyarray
34
35    Collects all the input values, including nulls, into an array.
36    Yes
37
38    array_agg ( anyarray ORDER BY input_sort_columns ) → anyarray
39
40    Concatenates all the input arrays into an array of one higher
41    dimension. (The inputs must all have the same dimensionality, and
42    cannot be empty or null.)
43    Yes
44
45    avg ( smallint ) → numeric
46
47    avg ( integer ) → numeric
48
49    avg ( bigint ) → numeric
50
51    avg ( numeric ) → numeric
52
53    avg ( real ) → double precision
54
55    avg ( double precision ) → double precision
56
57    avg ( interval ) → interval
58
59    Computes the average (arithmetic mean) of all the non-null input
60    values.
61    Yes
62
63    bit_and ( smallint ) → smallint
64
65    bit_and ( integer ) → integer
66
67    bit_and ( bigint ) → bigint
68
69    bit_and ( bit ) → bit
70
71    Computes the bitwise AND of all non-null input values.
72    Yes
73
74    bit_or ( smallint ) → smallint
75
76    bit_or ( integer ) → integer
77
78    bit_or ( bigint ) → bigint
79
80    bit_or ( bit ) → bit
81
82    Computes the bitwise OR of all non-null input values.
83    Yes
84
85    bit_xor ( smallint ) → smallint
86
87    bit_xor ( integer ) → integer
88
89    bit_xor ( bigint ) → bigint
90
91    bit_xor ( bit ) → bit
92
93    Computes the bitwise exclusive OR of all non-null input values. Can be
94    useful as a checksum for an unordered set of values.
95    Yes
96
97    bool_and ( boolean ) → boolean
98
99    Returns true if all non-null input values are true, otherwise false.
100    Yes
101
102    bool_or ( boolean ) → boolean
103
104    Returns true if any non-null input value is true, otherwise false.
105    Yes
106
107    count ( * ) → bigint
108
109    Computes the number of input rows.
110    Yes
111
112    count ( "any" ) → bigint
113
114    Computes the number of input rows in which the input value is not null.
115    Yes
116
117    every ( boolean ) → boolean
118
119    This is the SQL standard's equivalent to bool_and.
120    Yes
121
122    json_agg ( anyelement ORDER BY input_sort_columns ) → json
123
124    jsonb_agg ( anyelement ORDER BY input_sort_columns ) → jsonb
125
126    Collects all the input values, including nulls, into a JSON array.
127    Values are converted to JSON as per to_json or to_jsonb.
128    No
129
130    json_agg_strict ( anyelement ) → json
131
132    jsonb_agg_strict ( anyelement ) → jsonb
133
134    Collects all the input values, skipping nulls, into a JSON array.
135    Values are converted to JSON as per to_json or to_jsonb.
136    No
137
138    json_arrayagg ( [ value_expression ] [ ORDER BY sort_expression ] [ {
139    NULL | ABSENT } ON NULL ] [ RETURNING data_type [ FORMAT JSON [
140    ENCODING UTF8 ] ] ])
141
142    Behaves in the same way as json_array but as an aggregate function so
143    it only takes one value_expression parameter. If ABSENT ON NULL is
144    specified, any NULL values are omitted. If ORDER BY is specified, the
145    elements will appear in the array in that order rather than in the
146    input order.
147
148    SELECT json_arrayagg(v) FROM (VALUES(2),(1)) t(v) → [2, 1]
149    No
150
151    json_objectagg ( [ { key_expression { VALUE | ':' } value_expression }
152    ] [ { NULL | ABSENT } ON NULL ] [ { WITH | WITHOUT } UNIQUE [ KEYS ] ]
153    [ RETURNING data_type [ FORMAT JSON [ ENCODING UTF8 ] ] ])
154
155    Behaves like json_object, but as an aggregate function, so it only
156    takes one key_expression and one value_expression parameter.
157
158    SELECT json_objectagg(k:v) FROM (VALUES
159    ('a'::text,current_date),('b',current_date + 1)) AS t(k,v) → { "a" :
160    "2022-05-10", "b" : "2022-05-11" }
161    No
162
163    json_object_agg ( key "any", value "any" ORDER BY input_sort_columns )
164    → json
165
166    jsonb_object_agg ( key "any", value "any" ORDER BY input_sort_columns )
167    → jsonb
168
169    Collects all the key/value pairs into a JSON object. Key arguments are
170    coerced to text; value arguments are converted as per to_json or
171    to_jsonb. Values can be null, but keys cannot.
172    No
173
174    json_object_agg_strict ( key "any", value "any" ) → json
175
176    jsonb_object_agg_strict ( key "any", value "any" ) → jsonb
177
178    Collects all the key/value pairs into a JSON object. Key arguments are
179    coerced to text; value arguments are converted as per to_json or
180    to_jsonb. The key can not be null. If the value is null then the entry
181    is skipped,
182    No
183
184    json_object_agg_unique ( key "any", value "any" ) → json
185
186    jsonb_object_agg_unique ( key "any", value "any" ) → jsonb
187
188    Collects all the key/value pairs into a JSON object. Key arguments are
189    coerced to text; value arguments are converted as per to_json or
190    to_jsonb. Values can be null, but keys cannot. If there is a duplicate
191    key an error is thrown.
192    No
193
194    json_object_agg_unique_strict ( key "any", value "any" ) → json
195
196    jsonb_object_agg_unique_strict ( key "any", value "any" ) → jsonb
197
198    Collects all the key/value pairs into a JSON object. Key arguments are
199    coerced to text; value arguments are converted as per to_json or
200    to_jsonb. The key can not be null. If the value is null then the entry
201    is skipped. If there is a duplicate key an error is thrown.
202    No
203
204    max ( see text ) → same as input type
205
206    Computes the maximum of the non-null input values. Available for any
207    numeric, string, date/time, or enum type, as well as bytea, inet,
208    interval, money, oid, pg_lsn, tid, xid8, and also arrays and composite
209    types containing sortable data types.
210    Yes
211
212    min ( see text ) → same as input type
213
214    Computes the minimum of the non-null input values. Available for any
215    numeric, string, date/time, or enum type, as well as bytea, inet,
216    interval, money, oid, pg_lsn, tid, xid8, and also arrays and composite
217    types containing sortable data types.
218    Yes
219
220    range_agg ( value anyrange ) → anymultirange
221
222    range_agg ( value anymultirange ) → anymultirange
223
224    Computes the union of the non-null input values.
225    No
226
227    range_intersect_agg ( value anyrange ) → anyrange
228
229    range_intersect_agg ( value anymultirange ) → anymultirange
230
231    Computes the intersection of the non-null input values.
232    No
233
234    string_agg ( value text, delimiter text ) → text
235
236    string_agg ( value bytea, delimiter bytea ORDER BY input_sort_columns )
237    → bytea
238
239    Concatenates the non-null input values into a string. Each value after
240    the first is preceded by the corresponding delimiter (if it's not
241    null).
242    Yes
243
244    sum ( smallint ) → bigint
245
246    sum ( integer ) → bigint
247
248    sum ( bigint ) → numeric
249
250    sum ( numeric ) → numeric
251
252    sum ( real ) → real
253
254    sum ( double precision ) → double precision
255
256    sum ( interval ) → interval
257
258    sum ( money ) → money
259
260    Computes the sum of the non-null input values.
261    Yes
262
263    xmlagg ( xml ORDER BY input_sort_columns ) → xml
264
265    Concatenates the non-null XML input values (see Section 9.15.1.8).
266    No
267
268    It should be noted that except for count, these functions return a null
269    value when no rows are selected. In particular, sum of no rows returns
270    null, not zero as one might expect, and array_agg returns null rather
271    than an empty array when there are no input rows. The coalesce function
272    can be used to substitute zero or an empty array for null when
273    necessary.
274
275    The aggregate functions array_agg, json_agg, jsonb_agg,
276    json_agg_strict, jsonb_agg_strict, json_object_agg, jsonb_object_agg,
277    json_object_agg_strict, jsonb_object_agg_strict,
278    json_object_agg_unique, jsonb_object_agg_unique,
279    json_object_agg_unique_strict, jsonb_object_agg_unique_strict,
280    string_agg, and xmlagg, as well as similar user-defined aggregate
281    functions, produce meaningfully different result values depending on
282    the order of the input values. This ordering is unspecified by default,
283    but can be controlled by writing an ORDER BY clause within the
284    aggregate call, as shown in Section 4.2.7. Alternatively, supplying the
285    input values from a sorted subquery will usually work. For example:
286 SELECT xmlagg(x) FROM (SELECT x FROM test ORDER BY y DESC) AS tab;
287
288    Beware that this approach can fail if the outer query level contains
289    additional processing, such as a join, because that might cause the
290    subquery's output to be reordered before the aggregate is computed.
291
292 Note
293
294    The boolean aggregates bool_and and bool_or correspond to the standard
295    SQL aggregates every and any or some. PostgreSQL supports every, but
296    not any or some, because there is an ambiguity built into the standard
297    syntax:
298 SELECT b1 = ANY((SELECT b2 FROM t2 ...)) FROM t1 ...;
299
300    Here ANY can be considered either as introducing a subquery, or as
301    being an aggregate function, if the subquery returns one row with a
302    Boolean value. Thus the standard name cannot be given to these
303    aggregates.
304
305 Note
306
307    Users accustomed to working with other SQL database management systems
308    might be disappointed by the performance of the count aggregate when it
309    is applied to the entire table. A query like:
310 SELECT count(*) FROM sometable;
311
312    will require effort proportional to the size of the table: PostgreSQL
313    will need to scan either the entire table or the entirety of an index
314    that includes all rows in the table.
315
316    Table 9.63 shows aggregate functions typically used in statistical
317    analysis. (These are separated out merely to avoid cluttering the
318    listing of more-commonly-used aggregates.) Functions shown as accepting
319    numeric_type are available for all the types smallint, integer, bigint,
320    numeric, real, and double precision. Where the description mentions N,
321    it means the number of input rows for which all the input expressions
322    are non-null. In all cases, null is returned if the computation is
323    meaningless, for example when N is zero.
324
325    Table 9.63. Aggregate Functions for Statistics
326
327    Function
328
329    Description
330    Partial Mode
331
332    corr ( Y double precision, X double precision ) → double precision
333
334    Computes the correlation coefficient.
335    Yes
336
337    covar_pop ( Y double precision, X double precision ) → double precision
338
339    Computes the population covariance.
340    Yes
341
342    covar_samp ( Y double precision, X double precision ) → double
343    precision
344
345    Computes the sample covariance.
346    Yes
347
348    regr_avgx ( Y double precision, X double precision ) → double precision
349
350    Computes the average of the independent variable, sum(X)/N.
351    Yes
352
353    regr_avgy ( Y double precision, X double precision ) → double precision
354
355    Computes the average of the dependent variable, sum(Y)/N.
356    Yes
357
358    regr_count ( Y double precision, X double precision ) → bigint
359
360    Computes the number of rows in which both inputs are non-null.
361    Yes
362
363    regr_intercept ( Y double precision, X double precision ) → double
364    precision
365
366    Computes the y-intercept of the least-squares-fit linear equation
367    determined by the (X, Y) pairs.
368    Yes
369
370    regr_r2 ( Y double precision, X double precision ) → double precision
371
372    Computes the square of the correlation coefficient.
373    Yes
374
375    regr_slope ( Y double precision, X double precision ) → double
376    precision
377
378    Computes the slope of the least-squares-fit linear equation determined
379    by the (X, Y) pairs.
380    Yes
381
382    regr_sxx ( Y double precision, X double precision ) → double precision
383
384    Computes the “sum of squares” of the independent variable, sum(X^2) -
385    sum(X)^2/N.
386    Yes
387
388    regr_sxy ( Y double precision, X double precision ) → double precision
389
390    Computes the “sum of products” of independent times dependent
391    variables, sum(X*Y) - sum(X) * sum(Y)/N.
392    Yes
393
394    regr_syy ( Y double precision, X double precision ) → double precision
395
396    Computes the “sum of squares” of the dependent variable, sum(Y^2) -
397    sum(Y)^2/N.
398    Yes
399
400    stddev ( numeric_type ) → double precision for real or double
401    precision, otherwise numeric
402
403    This is a historical alias for stddev_samp.
404    Yes
405
406    stddev_pop ( numeric_type ) → double precision for real or double
407    precision, otherwise numeric
408
409    Computes the population standard deviation of the input values.
410    Yes
411
412    stddev_samp ( numeric_type ) → double precision for real or double
413    precision, otherwise numeric
414
415    Computes the sample standard deviation of the input values.
416    Yes
417
418    variance ( numeric_type ) → double precision for real or double
419    precision, otherwise numeric
420
421    This is a historical alias for var_samp.
422    Yes
423
424    var_pop ( numeric_type ) → double precision for real or double
425    precision, otherwise numeric
426
427    Computes the population variance of the input values (square of the
428    population standard deviation).
429    Yes
430
431    var_samp ( numeric_type ) → double precision for real or double
432    precision, otherwise numeric
433
434    Computes the sample variance of the input values (square of the sample
435    standard deviation).
436    Yes
437
438    Table 9.64 shows some aggregate functions that use the ordered-set
439    aggregate syntax. These functions are sometimes referred to as “inverse
440    distribution” functions. Their aggregated input is introduced by ORDER
441    BY, and they may also take a direct argument that is not aggregated,
442    but is computed only once. All these functions ignore null values in
443    their aggregated input. For those that take a fraction parameter, the
444    fraction value must be between 0 and 1; an error is thrown if not.
445    However, a null fraction value simply produces a null result.
446
447    Table 9.64. Ordered-Set Aggregate Functions
448
449    Function
450
451    Description
452    Partial Mode
453
454    mode () WITHIN GROUP ( ORDER BY anyelement ) → anyelement
455
456    Computes the mode, the most frequent value of the aggregated argument
457    (arbitrarily choosing the first one if there are multiple
458    equally-frequent values). The aggregated argument must be of a sortable
459    type.
460    No
461
462    percentile_cont ( fraction double precision ) WITHIN GROUP ( ORDER BY
463    double precision ) → double precision
464
465    percentile_cont ( fraction double precision ) WITHIN GROUP ( ORDER BY
466    interval ) → interval
467
468    Computes the continuous percentile, a value corresponding to the
469    specified fraction within the ordered set of aggregated argument
470    values. This will interpolate between adjacent input items if needed.
471    No
472
473    percentile_cont ( fractions double precision[] ) WITHIN GROUP ( ORDER
474    BY double precision ) → double precision[]
475
476    percentile_cont ( fractions double precision[] ) WITHIN GROUP ( ORDER
477    BY interval ) → interval[]
478
479    Computes multiple continuous percentiles. The result is an array of the
480    same dimensions as the fractions parameter, with each non-null element
481    replaced by the (possibly interpolated) value corresponding to that
482    percentile.
483    No
484
485    percentile_disc ( fraction double precision ) WITHIN GROUP ( ORDER BY
486    anyelement ) → anyelement
487
488    Computes the discrete percentile, the first value within the ordered
489    set of aggregated argument values whose position in the ordering equals
490    or exceeds the specified fraction. The aggregated argument must be of a
491    sortable type.
492    No
493
494    percentile_disc ( fractions double precision[] ) WITHIN GROUP ( ORDER
495    BY anyelement ) → anyarray
496
497    Computes multiple discrete percentiles. The result is an array of the
498    same dimensions as the fractions parameter, with each non-null element
499    replaced by the input value corresponding to that percentile. The
500    aggregated argument must be of a sortable type.
501    No
502
503    Each of the “hypothetical-set” aggregates listed in Table 9.65 is
504    associated with a window function of the same name defined in
505    Section 9.22. In each case, the aggregate's result is the value that
506    the associated window function would have returned for the
507    “hypothetical” row constructed from args, if such a row had been added
508    to the sorted group of rows represented by the sorted_args. For each of
509    these functions, the list of direct arguments given in args must match
510    the number and types of the aggregated arguments given in sorted_args.
511    Unlike most built-in aggregates, these aggregates are not strict, that
512    is they do not drop input rows containing nulls. Null values sort
513    according to the rule specified in the ORDER BY clause.
514
515    Table 9.65. Hypothetical-Set Aggregate Functions
516
517    Function
518
519    Description
520    Partial Mode
521
522    rank ( args ) WITHIN GROUP ( ORDER BY sorted_args ) → bigint
523
524    Computes the rank of the hypothetical row, with gaps; that is, the row
525    number of the first row in its peer group.
526    No
527
528    dense_rank ( args ) WITHIN GROUP ( ORDER BY sorted_args ) → bigint
529
530    Computes the rank of the hypothetical row, without gaps; this function
531    effectively counts peer groups.
532    No
533
534    percent_rank ( args ) WITHIN GROUP ( ORDER BY sorted_args ) → double
535    precision
536
537    Computes the relative rank of the hypothetical row, that is (rank - 1)
538    / (total rows - 1). The value thus ranges from 0 to 1 inclusive.
539    No
540
541    cume_dist ( args ) WITHIN GROUP ( ORDER BY sorted_args ) → double
542    precision
543
544    Computes the cumulative distribution, that is (number of rows preceding
545    or peers with hypothetical row) / (total rows). The value thus ranges
546    from 1/N to 1.
547    No
548
549    Table 9.66. Grouping Operations
550
551    Function
552
553    Description
554
555    GROUPING ( group_by_expression(s) ) → integer
556
557    Returns a bit mask indicating which GROUP BY expressions are not
558    included in the current grouping set. Bits are assigned with the
559    rightmost argument corresponding to the least-significant bit; each bit
560    is 0 if the corresponding expression is included in the grouping
561    criteria of the grouping set generating the current result row, and 1
562    if it is not included.
563
564    The grouping operations shown in Table 9.66 are used in conjunction
565    with grouping sets (see Section 7.2.4) to distinguish result rows. The
566    arguments to the GROUPING function are not actually evaluated, but they
567    must exactly match expressions given in the GROUP BY clause of the
568    associated query level. For example:
569 => SELECT * FROM items_sold;
570  make  | model | sales
571 -------+-------+-------
572  Foo   | GT    |  10
573  Foo   | Tour  |  20
574  Bar   | City  |  15
575  Bar   | Sport |  5
576 (4 rows)
577
578 => SELECT make, model, GROUPING(make,model), sum(sales) FROM items_sold GROUP BY
579  ROLLUP(make,model);
580  make  | model | grouping | sum
581 -------+-------+----------+-----
582  Foo   | GT    |        0 | 10
583  Foo   | Tour  |        0 | 20
584  Bar   | City  |        0 | 15
585  Bar   | Sport |        0 | 5
586  Foo   |       |        1 | 30
587  Bar   |       |        1 | 20
588        |       |        3 | 50
589 (7 rows)
590
591    Here, the grouping value 0 in the first four rows shows that those have
592    been grouped normally, over both the grouping columns. The value 1
593    indicates that model was not grouped by in the next-to-last two rows,
594    and the value 3 indicates that neither make nor model was grouped by in
595    the last row (which therefore is an aggregate over all the input rows).