9.3. General-purpose Aggregate Functions

9.3.1. ANY_VALUE()

Returns the expression for an arbitrary row in the group.

Result typeDepends on the input type

Syntax

ANY_VALUE ( <expr> )

Table 9.1. ANY_VALUE Function Parameters
ParameterDescription

expr

Expression of any type.

This is a non-deterministic aggregate function that returns the result of expr applied to an arbitrary row in the group.

If expr returns NULL for a row, another row is tried. NULL is only returned if expr evaluates to NULL for all rows in the group or the group is empty.

9.3.1.1. ANY_VALUE Examples

select
  DEPT_NO,
  any_value(FIRST_NAME || ' ' || LAST_NAME) "Example Employee"
from EMPLOYEE
group by DEPT_NO

See alsoSELECT

9.3.2. AVG()

Average

Result typeDepends on the input type

Syntax

AVG ([ALL | DISTINCT] <expr>)

Table 9.2. AVG Function Parameters
ParameterDescription

expr

Expression. It may contain a table column, a constant, a variable, an expression, a non-aggregate function or a UDF that returns a numeric data type. Aggregate functions are not allowed as expressions

AVG returns the average argument value in the group. NULL is ignored.

  • Parameter ALL (the default) applies the aggregate function to all values.

  • Parameter DISTINCT directs the AVG function to consider only one instance of each unique value, no matter how many times this value occurs.

  • If the set of retrieved records is empty or contains only NULL, the result will be NULL.

The result type of AVG depends on the input type:

FLOAT, DOUBLE PRECISION

DOUBLE PRECISION

SMALLINT, INTEGER, BIGINT

BIGINT

INT128

INT128

DECIMAL/NUMERIC(p, n) with p < 19

DECIMAL/NUMERIC(18, n)

DECIMAL/NUMERIC(p, n) with p >= 19

DECIMAL/NUMERIC(38, n)

DECFLOAT(16)

DECFLOAT(16)

DECFLOAT(34)

DECFLOAT(34)

9.3.2.1. AVG Examples

SELECT
  dept_no,
  AVG(salary)
FROM employee
GROUP BY dept_no

See alsoSELECT

9.3.3. COUNT()

Counts non-NULL values

Result typeBIGINT

Syntax

COUNT ([ALL | DISTINCT] <expr> | *)

Table 9.3. COUNT Function Parameters
ParameterDescription

expr

Expression. It may contain a table column, a constant, a variable, an expression, a non-aggregate function or a UDF that returns a numeric data type. Aggregate functions are not allowed as expressions

COUNT returns the number of non-null values in a group.

  • ALL is the default: it counts all values in the set that are not NULL.

  • If DISTINCT is specified, duplicates are excluded from the counted set.

  • If COUNT (*) is specified instead of the expression expr, all rows will be counted. COUNT (*) — 

    • does not accept parameters

    • cannot be used with the keyword DISTINCT

    • does not take an expr argument, since its context is column-unspecific by definition

    • counts each row separately and returns the number of rows in the specified table or group without omitting duplicate rows

    • counts rows containing NULL

  • If the result set is empty or contains only NULL in the specified column(s), the returned count is zero.

9.3.3.1. COUNT Examples

SELECT
  dept_no,
  COUNT(*) AS cnt,
  COUNT(DISTINCT name) AS cnt_name
FROM employee
GROUP BY dept_no

See alsoSELECT

9.3.4. LIST()

Since Firebird 6.0, LIST() is an alias for the SQL standard function Section 9.3.5, “LISTAGG()”.

9.3.5. LISTAGG()

Concatenates values into a string list

Result typeBLOB

Syntax

<listagg-set-function> ::=
  { LISTAGG | LIST }
    <left-paren>
      [ ALL | DISTINCT ] <expr>
      [ <comma> listagg-separator ]
      [ <listagg-overflow-clause> ]
    <right-paren>
    [ <within-group-specification> ]
 
<listagg-overflow-clause> ::=
  ON OVERFLOW <overflow-behavior>
 
<overflow-behavior> ::=
    ERROR
  | TRUNCATE [ listagg-truncation-filler ] <listagg-count-indication>
 
<listagg-count-indication> ::=
  WITH COUNT | WITHOUT COUNT
 
<within-group-specification> ::=
  !! See Section 9.2, “WITHIN GROUP Clause for Aggregate Functions” !!

Table 9.4. LISTAGG Function Parameters
ParameterDescription

expr

Expression (e.g. column reference) to concatenate.

listagg-separator

Expression for the separator. Defaults to ,.

listagg-truncation-filler

Truncation filler on overflow. Defaults to ....

LISTAGG returns a string consisting of the non-NULL argument values in the group, separated either by a comma or by listagg-separator. If there are no non-NULL values or if the group is empty, NULL is returned.

LISTAGG is defined by the SQL standard. For backward compatibility with Firebird 5.0 and older, LIST is an alias for LISTAGG. Compared to the SQL standard, some clauses and arguments are optional so LISTAGG and LIST can share the same definition.

  • ALL (the default) results in all non-NULL values being listed. With DISTINCT, duplicates are removed, except if expr is a BLOB.

  • The optional listagg-separator argument may be any expression convertible to string. This makes it possible to specify e.g. ascii_char(13) as a separator.

  • The expr and listagg-separator arguments support BLOBs of any size and character set.

  • Non-string arguments are implicitly converted to strings before concatenation.

  • The result is a text BLOB, except when expr is a BLOB of another subtype.

  • The order of concatenation of expr is only defined when the WITHIN GROUP clause is specified. Otherwise, the order is undefined and depends on implementation details of the optimizer and the selected execution plan.

    ☞
    Tip

    In Firebird 5.0 and older, ordering the concatenation performed by LIST was — sometimes — possible by using an ordered derived table. We highly recommend replacing such brittle workarounds with the WITHIN GROUP clause.

Deviations from the SQL standard definition of LISTAGG
  • expr and listagg-separator can be expressions of any type convertible to string.

    The SQL standard only accepts a string literal for listagg-separator.

  • listagg-separator is optional, defaulting to a comma (,), for backward compatibility with LIST.

  • The WITHIN GROUP clause is optional, for backward compatibility with LIST.

  • The OVERFLOW clause is ignored as the return type is always a BLOB, so it’s considered never to overflow. It’s only provided for syntax compatibility with the SQL standard.

9.3.5.1. LISTAGG Examples

  1. Retrieving the list, order undefined:

    select
      listagg(DISPLAY_NAME, '; ')
    from GR_WORK;
  2. Retrieving the list in alphabetical order:

    select
      listagg(DISPLAY_NAME, '; ') within group (order by DISPLAY_NAME)
    from GR_WORK;
  3. Same as previous, but eliminate duplicates:

    select
      listagg(distinct DISPLAY_NAME, '; ') within group (order by DISPLAY_NAME)
    from GR_WORK;

See alsoSection 9.2, “WITHIN GROUP Clause for Aggregate Functions”, SELECT, Section 10.1.2, “UNLIST()”

9.3.6. MAX()

Maximum

Result typeReturns a result of the same data type the input expression.

Syntax

MAX ([ALL | DISTINCT] <expr>)

Table 9.5. MAX Function Parameters
ParameterDescription

expr

Expression. It may contain a table column, a constant, a variable, an expression, a non-aggregate function or a UDF. Aggregate functions are not allowed as expressions.

MAX returns the maximum non-NULL element in the result set.

  • If the group is empty or contains only NULLs, the result is NULL.

  • If the input argument is a string, the function will return the value that will be sorted last if COLLATE is used.

  • This function fully supports text BLOBs of any size and character set.

9.3.6.1. MAX Examples

SELECT
  dept_no,
  MAX(salary)
FROM employee
GROUP BY dept_no

See alsoSection 9.3.7, “MIN()”, SELECT

9.3.7. MIN()

Minimum

Result typeReturns a result of the same data type the input expression.

Syntax

MIN ([ALL | DISTINCT] <expr>)

Table 9.6. MIN Function Parameters
ParameterDescription

expr

Expression. It may contain a table column, a constant, a variable, an expression, a non-aggregate function or a UDF. Aggregate functions are not allowed as expressions.

MIN returns the minimum non-NULL element in the result set.

  • If the group is empty or contains only NULLs, the result is NULL.

  • If the input argument is a string, the function will return the value that will be sorted first if COLLATE is used.

  • This function fully supports text BLOBs of any size and character set.

9.3.7.1. MIN Examples

SELECT
  dept_no,
  MIN(salary)
FROM employee
GROUP BY dept_no

See alsoSection 9.3.6, “MAX()”, SELECT

9.3.8. SUM()

Sum

Result typeDepends on the input type

Syntax

SUM ([ALL | DISTINCT] <expr>)

Table 9.7. SUM Function Parameters
ParameterDescription

expr

Numeric expression. It may contain a table column, a constant, a variable, an expression, a non-aggregate function or a UDF. Aggregate functions are not allowed as expressions.

SUM calculates and returns the sum of non-NULL values in the group.

  • If the group is empty or contains only NULLs, the result is NULL.

  • ALL is the default option — all values in the set that are not NULL are processed. If DISTINCT is specified, duplicates are removed from the set and the SUM evaluation is done afterward.

The result type of SUM depends on the input type:

FLOAT, DOUBLE PRECISION

DOUBLE PRECISION

SMALLINT, INTEGER

BIGINT

BIGINT, INT128

INT128

DECIMAL/NUMERIC(p, n) with p < 10

DECIMAL/NUMERIC(18, n)

DECIMAL/NUMERIC(p, n) with p >= 10

DECIMAL/NUMERIC(38, n)

DECFLOAT(16), DECFLOAT(34)

DECFLOAT(34)

9.3.8.1. SUM Examples

SELECT
  dept_no,
  SUM (salary),
FROM employee
GROUP BY dept_no

See alsoSELECT