Aggregate Functions
- reference
Aggregate functions take multiple values from documents, perform calculations, and return a single value as the result. The function names are case insensitive.
You can only use aggregate functions in SELECT
, LETTING
, HAVING
, and ORDER BY
clauses.
When using an aggregate function in a query, the query operates as an aggregate query.
In Couchbase Server Enterprise Edition, aggregate functions can also be used as window functions when they are used with a window specification, which is introduced by the OVER
keyword.
In Couchbase Server 7.0 and later, window functions (and aggregate functions used as window functions) may specify their own inline window definitions, or they may refer to a named window defined by the WINDOW clause elsewhere in the query. By defining a named window with the WINDOW clause, you can reuse the window definition across several functions in the query, potentially making the query easier to write and maintain.
Syntax
This section describes the generic syntax of aggregate functions, including the OVER clause. See the sections below for details of individual aggregate functions.
Aggregate Functions
aggregate-function ::= function-name '(' [ aggregate-quantifier ] expr ')' [ filter-clause ]

Aggregate Functions as Window Functions
aggregate-function-window ::= function-name '(' [ aggregate-quantifier ] expr ')' [ filter-clause ] over-clause

Arguments
Aggregate functions take one argument, which is used to compute the aggregate function.
The COUNT
function can also take a wildcard (*) or a path with a wildcard (path.*) as its argument.
Aggregate Quantifier
aggregate-quantifier ::= ALL | DISTINCT

The aggregate quantifier determines whether the function aggregates all values in the group, or distinct values only.
ALL
-
All objects are included in the computation.
DISTINCT
-
Only distinct objects are included in the computation.
This quantifier can only be used with aggregate functions.
This quantifier is optional.
If omitted, the default value is ALL
.
FILTER Clause
(Introduced in Couchbase Server 7.0)
filter-clause ::= FILTER '(' WHERE cond ')'

The FILTER clause enables you to specify which values are included in the aggregate. This clause is available for aggregate functions, and aggregate functions used as window functions. (It is not permitted for dedicated window functions.)
The FILTER clause is useful when a query contains several aggregate functions, each of which requires a different condition.
- cond
-
[Required] Conditional expression. Values for which the condition resolves to TRUE are included in the aggregation.
The conditional expression is subject to the same rules as the conditional expression in the query WHERE clause, and the same rules as aggregation operands. It may not contain a subquery, a window function, or an outer reference.
If the query block contains an aggregate function which uses the FILTER clause, the aggregation is not pushed down to the indexer. Refer to Grouping and Aggregate Pushdown for more details. |
OVER Clause
over-clause ::= OVER ( window-ref | '(' window-definition ')' )

The OVER clause introduces the window specification for the function. There are two ways of specifying the window.
-
An inline window definition specifies the window directly within the function call. It is delimited by parentheses
()
and has exactly the same syntax as the window definition in a WINDOW clause. For further details, refer to Window Definition. -
A window reference refers to a named window, which must be defined by a WINDOW clause in the same query block as the function call. For further details, refer to WINDOW Clause.
ARRAY_AGG( [ ALL | DISTINCT ] expression
)
AVG( [ ALL | DISTINCT ] expression
)
This function has an alias MEAN().
Return Value
With the ALL
quantifier, or no quantifier, returns the arithmetic mean (average) of all the number values in the group.
With the DISTINCT
quantifier, returns the arithmetic mean (average) of all the distinct number values in the group.
Returns NULL if there are no number values in the group.
COUNT( [ ALL | DISTINCT ] expression
)
Return Value
With the ALL
quantifier, or no quantifier, returns count of all the non-NULL and non-MISSING values in the group. [1]
With the DISTINCT
quantifier, returns count of all the distinct non-NULL and non-MISSING values in the group.
COUNTN( [ ALL | DISTINCT ] expression
)
Return Value
With the ALL
quantifier, or no quantifier, returns a count of all the numeric values in the group. [1]
With the DISTINCT
quantifier, returns a count of all the distinct numeric values in the group.
MAX( [ ALL | DISTINCT ] expression
)
Return Value
Returns the maximum non-NULL, non-MISSING value in the group in N1QL collation order.
This function returns the same result with the ALL
quantifier, the DISTINCT
quantifier, or no quantifier.
MEAN( [ ALL | DISTINCT ] expression
)
Alias for AVG().
MEDIAN( [ ALL | DISTINCT ] expression
)
Return Value
With the ALL
quantifier, or no quantifier, returns the median of all the number values in the group.
If there is an even number of number values, returns the mean of the median two values.
With the DISTINCT
quantifier, returns the median of all the distinct number values in the group.
If there is an even number of distinct number values, returns the mean of the median two values.
Returns NULL if there are no number values in the group.
MIN( [ ALL | DISTINCT ] expression
)
Return Value
Returns the minimum non-NULL, non-MISSING value in the group in N1QL collation order.
This function returns the same result with the ALL
quantifier, the DISTINCT
quantifier, or no quantifier.
Examples
That result might have been surprising since some symbols come before letters and are therefore "lower" than letters. To avoid this symbol confusion, you can specify letters only, as in the following example.
Min of a string field, regardless of preceding non-letters.
Find the first hotel alphabetically in the hotel
keyspace.
STDDEV( [ ALL | DISTINCT ] expression
)
Return Value
With the ALL
quantifier, or no quantifier, returns the corrected sample standard deviation of all the number values in the group.
With the DISTINCT
quantifier, returns the corrected sample standard deviation of all the distinct number values in the group.
Returns NULL if there are no number values in the group.
Examples
Sample standard deviation of a single value.
n1qlSELECT STDDEV(reviews[0].ratings.Cleanliness) AS StdDevSingle
FROM `travel-sample`.inventory.hotel
WHERE name="Sachas Hotel";
json[
{
"StdDevSingle": 0 (1)
}
]
1 | There is only one matching result in the input, so the function returns 0 . |
STDDEV_POP( [ ALL | DISTINCT ] expression
)
Return Value
With the ALL
quantifier, or no quantifier, returns the population standard deviation of all the number values in the group.
With the DISTINCT
quantifier, returns the population standard deviation of all the distinct number values in the group.
Returns NULL if there are no number values in the group.
STDDEV_SAMP( [ ALL | DISTINCT ] expression
)
A near-synonym for STDDEV().
The only difference is that STDDEV_SAMP()
returns NULL if there is only one matching element.
Example
Sample standard deviation of a single value.
n1qlSELECT STDDEV_SAMP(reviews[0].ratings.Cleanliness) AS StdDevSingle
FROM `travel-sample`.inventory.hotel
WHERE name="Sachas Hotel";
json[
{
"StdDevSamp": null (1)
}
]
1 | There is only one matching result in the input, so the function returns NULL. |
SUM( [ ALL | DISTINCT ] expression
)
Return Value
With the ALL
quantifier, or no quantifier, returns the sum of all the number values in the group.
With the DISTINCT
quantifier, returns the arithmetic sum of all the distinct number values in the group.
Returns NULL if there are no number values in the group.
Examples
The sum total of all airline route stops in the route
keyspace.
In the route keyspace, nearly all flights are non-stop (0 stops) and only six flights have 1 stop, so we expect 6 flights of 1 stop each, a total of 6.
|
n1qlSELECT SUM(stops) AS SumOfStops FROM `travel-sample`.inventory.route;
json[
{
"SumOfStops": 6 (1)
}
]
1 | There are 6 routes with 1 stop each. |
VARIANCE( [ ALL | DISTINCT ] expression
)
Return Value
With the ALL
quantifier, or no quantifier, returns the unbiased sample variance (the square of the corrected sample standard deviation) of all the number values in the group.
With the DISTINCT
quantifier, returns the unbiased sample variance (the square of the corrected sample standard deviation) of all the distinct number values in the group.
Returns NULL if there are no number values in the group.
This function has a near-synonym VARIANCE_SAMP().
The only difference is that VARIANCE()
returns NULL if there is only one matching element.
VARIANCE_POP( [ ALL | DISTINCT ] expression
)
This function has an alias VAR_POP().
Return Value
With the ALL
quantifier, or no quantifier, returns the population variance (the square of the population standard deviation) of all the number values in the group.
With the DISTINCT
quantifier, returns the population variance (the square of the population standard deviation) of all the distinct number values in the group.
Returns NULL if there are no number values in the group.
VARIANCE_SAMP( [ ALL | DISTINCT ] expression
)
A near-synonym for VARIANCE().
The only difference is that VARIANCE_SAMP()
returns NULL if there is only one matching element.
This function has an alias VAR_SAMP().
Example
Sample standard deviation of a single value.
n1qlSELECT VARIANCE_SAMP(reviews[0].ratings.Cleanliness) AS VarianceSamp
FROM `travel-sample`.inventory.hotel
WHERE name="Sachas Hotel";
json[
{
"VarianceSamp": null (1)
}
]
1 | There is only one matching result in the input, so the function returns NULL. |
VAR_POP( [ ALL | DISTINCT ] expression
)
Alias for VARIANCE_POP().
VAR_SAMP( [ ALL | DISTINCT ] expression
)
Alias for VARIANCE_SAMP().
Formulas
The corrected sample standard deviation is calculated according to the following formula.

The population standard deviation is calculated according to the following formula.

Related Links
-
GROUP BY Clause for GROUP BY, LETTING, and HAVING clauses.
-
WINDOW Clause for WINDOW clauses.
-
Window Functions for window functions.