9.2 General-purpose Aggregate Functions
9.2.1 AVG()
Available inDSQL, ESQL, PSQL
Result typeDepends on the input type
Syntax
1. AVG ([ALL | DISTINCT] <expr>)
Table 9.2.1.1 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 |
AVG returns the average argument value in the group. NULL is ignored.
- Parameter
ALL(the default) applies the aggregate function to all values. - Parameter
DISTINCTdirects theAVGfunction 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 beNULL.
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) |
Note
In Firebird 4.0.0, the result type is determined the same as Section 9.2.6, SUM(). This was fixed in 4.0.1, see firebird#6845.
9.2.1.1 AVG Examples
1. SELECT
2. dept_no,
3. AVG(salary)
4. FROM employee
5. GROUP BY dept_no
See alsoSELECT
9.2.2 COUNT()
Available inDSQL, ESQL, PSQL
Result typeBIGINT
Syntax
1. COUNT ([ALL | DISTINCT] <expr> | *)
Table 9.2.2.1 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 |
COUNT returns the number of non-null values in a group.
ALLis the default: it simply counts all values in the set that are notNULL.- If
DISTINCTis 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
NULLin the specified column(s), the returned count is zero.
9.2.2.1 COUNT Examples
1. SELECT
2. dept_no,
3. COUNT(*) AS cnt,
4. COUNT(DISTINCT name) AS cnt_name
5. FROM employee
6. GROUP BY dept_no
See alsoSELECT.
9.2.3 LIST()
Available inDSQL, PSQL
Result typeBLOB
Syntax
1. LIST ([ALL | DISTINCT] <expr> [, separator ])
Table 9.2.3.1 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 |
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-NULLvalues being listed. WithDISTINCT, duplicates are removed, except if expr is aBLOB.- In Firebird 2.5 and up, the optional separator argument may be any string expression. This makes it possible to specify e.g.
ascii_char(13)as a separator. (This improvement has also been backported to 2.1.4.) - The expr 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 expr is aBLOBof 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.
9.2.3.1 LIST Examples
- Retrieving the list, order undefined:
1. SELECT LIST (display_name, '; ') FROM GR_WORK;
2. Retrieving the list in alphabetical order, using a derived table:
1. SELECT LIST (display_name, '; ')
2. FROM (SELECT display_name
3. FROM GR_WORK
4. ORDER BY display_name);
See alsoSELECT
9.2.4 MAX()
Available inDSQL, ESQL, PSQL
Result typeReturns a result of the same data type the input expression.
Syntax
1. MAX ([ALL | DISTINCT] <expr>)
Table 9.2.4.1 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. |
MAX returns the maximum non-NULL element in the result set.
- If the group is empty or contains only
NULLs, the result isNULL. - If the input argument is a string, the function will return the value that will be sorted last if
COLLATEis used. - This function fully supports text
BLOBs of any size and character set.
Note
The DISTINCT parameter makes no sense if used with MAX() and is implemented only for compliance with the standard.
9.2.4.1 MAX Examples
1. SELECT
2. dept_no,
3. MAX(salary)
4. FROM employee
5. GROUP BY dept_no
See alsoSection 9.2.5, MIN(), SELECT
9.2.5 MIN()
Available inDSQL, ESQL, PSQL
Result typeReturns a result of the same data type the input expression.
Syntax
1. MIN ([ALL | DISTINCT] <expr>)
Table 9.2.5.1 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. |
MIN returns the minimum non-NULL element in the result set.
- If the group is empty or contains only
NULLs, the result isNULL. - If the input argument is a string, the function will return the value that will be sorted first if
COLLATEis used. - This function fully supports text
BLOBs of any size and character set.
Note
The DISTINCT parameter makes no sense if used with MIN() and is implemented only for compliance with the standard.
9.2.5.1 MIN Examples
1. SELECT
2. dept_no,
3. MIN(salary)
4. FROM employee
5. GROUP BY dept_no
See alsoSection 9.2.4, MAX(), SELECT
9.2.6 SUM()
Available inDSQL, ESQL, PSQL
Result typeDepends on the input type
Syntax
1. SUM ([ALL | DISTINCT] <expr>)
Table 9.2.6.1 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. |
SUM calculates and returns the sum of non-null values in the group.
- If the group is empty or contains only
NULLs, the result isNULL. - ALL is the default option — all values in the set that are not
NULLare processed. IfDISTINCTis specified, duplicates are removed from the set and theSUMevaluation is done afterwards.
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.2.6.1 SUM Examples
1. SELECT
2. dept_no,
3. SUM (salary),
4. FROM employee
5. GROUP BY dept_no
See alsoSELECT
