Firebird 2.5 Language Reference → Firebird 2.5 Language Reference → Built-in functions and Variables → Aggregate Functions |
Aggregate functions operate on groups of records, rather than on individual records or variables. They are often used in combination with a GROUP BY clause.
Available in: DSQL, ESQL, PSQL
Syntax:
AVG ([ALL | DISTINCT] <expr>)
Table 8.69. AVG Function Parameters
Parameter | Description |
---|---|
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 |
Description: AVG returns the average argument value in the group. NULL is ignored.
NULL
, the result will be NULL
.
Result type: A numeric data type, the same as the data type of the argument.
Syntax:
AVG (expression
)
If the group is empty or contains only NULLs, the result is NULL.
Example:
SELECT dept_no, AVG(salary) FROM employee GROUP BY dept_no
See also: SELECT
Available in: DSQL, ESQL, PSQL
Syntax:
COUNT ([ALL | DISTINCT] <expr> | *)
Table 8.70. COUNT Function Parameters
Parameter | Description |
---|---|
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 |
Result type: Integer
Description: COUNT returns the number of non-null values in a group.
NULL
.
NULL
NULL
in
the specified column[s], the returned count is zero.
Example:
SELECT dept_no, COUNT(*) AS cnt, COUNT(DISTINCT name) AS cnt_name FROM employee GROUP BY dept_no
See also: SELECT.
Available in: DSQL, PSQL
Changed in: 2.5
Syntax:
LIST ([ALL | DISTINCT]expression
[,separator
])
Table 8.71. LIST Function Parameters
Parameter | Description |
---|---|
expr | Expression. It may contain a table column, a constant, a variable, an expression, a non-aggregate function or a UDF that returns the string data type or a BLOB. Fields of numeric and date/time types are converted to strings. Aggregate functions are not allowed as expressions |
separator | Optional alternative separator, a string expression. Comma is the default separator |
Result type: BLOB
Description: LIST returns a string consisting of the
non-NULL
argument values in the group, separated either by a comma or
by a user-supplied separator. If there are no non-NULL
values (this
includes the case where the group is empty), NULL
is returned.
ALL (the default) results in all
non-NULL
values being listed. With
DISTINCT, duplicates are removed, except if
expression
is a BLOB.
In Firebird 2.5 and up, the optional separator
argument may be any string expression. This makes it possible to specify e.g.
as a separator. (This
improvement has also been backported to 2.1.4.)
ascii_char(13)
The expression
and
separator
arguments support BLOBs of
any size and character set.
Date/time and numeric arguments are implicitly converted to strings before concatenation.
The result is a text BLOB, except when
expression
is a BLOB of another
subtype.
The ordering of the list values is undefined—the order in which the strings are concatenated is determined by read order from the source set which, in tables, is not generally defined. If ordering is important, the source data can be pre-sorted using a derived table or similar.
Examples:
SELECT LIST (display_name, '; ') FROM GR_WORK;
SELECT LIST (display_name, '; ') FROM (SELECT display_name FROM GR_WORK ORDER BY display_name);
See also: SELECT
Available in: DSQL, ESQL, PSQL
Syntax:
MAX ([ALL | DISTINCT]<expr>
)
Table 8.72. MAX Function Parameters
Parameter | Description |
---|---|
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. |
Result type: Returns a result of the same data type the input expression.
Description: MAX returns the maximum non-NULL
element
in the result set.
If the group is empty or contains only NULLs, the result is NULL.
Note | |
---|---|
The DISTINCT parameter makes no sense if used with MAX() and is implemented only for compliance with the standard. |
Example:
SELECT dept_no, MAX(salary) FROM employee GROUP BY dept_no
Available in: DSQL, ESQL, PSQL
Syntax:
MIN ([ALL | DISTINCT]<expr>
)
Table 8.73. MIN Function Parameters
Parameter | Description |
---|---|
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. |
Result type: Returns a result of the same data type the input expression.
Description: MIN returns the minimum non-NULL
element
in the result set.
If the group is empty or contains only NULLs, the result is NULL.
Note | |
---|---|
The DISTINCT parameter makes no sense if used with MIN() and is implemented only for compliance with the standard. |
Example:
SELECT dept_no, MIN(salary) FROM employee GROUP BY dept_no
Available in: DSQL, ESQL, PSQL
Syntax:
SUM ([ALL | DISTINCT]<expr>
)
Table 8.74. SUM Function Parameters
Parameter | Description |
---|---|
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. |
Result type: Returns a result of the same numeric data type as the input expression.
Description: 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.
Example: SELECT dept_no, SUM (salary), FROM employee GROUP BY dept_no
See also: SELECT
Firebird 2.5 Language Reference → Firebird 2.5 Language Reference → Built-in functions and Variables → Aggregate Functions |