Count function
This function can be used when counting field values and expressions.
| Function | Description |
|---|---|
count() | Counts a row, or whether a given value is truthy |
count
The count function counts the number of times that the function is called. This is useful for returning the total number of rows in a SELECT statement with a GROUP BY clause.
If a value is given as the first argument, then this function checks whether a given value is truthy. This is useful for returning the total number of rows, which match a certain condition, in a SELECT statement, with a GROUP BY clause.
If an array is given, this function counts the number of items in the array which are truthy. If, instead, you want to count the total number of items in the given array, then use the array::len() function.
The following example shows this function, and its output, when used in a RETURN statement:
The following examples show this function being used in a SELECT statement with a GROUP clause:
An advanced example of the count function can be seen below:
Using a COUNT index with count()
A COUNT index can be defined to speed up count() when used with a GROUP ALL clause. This allows count() to access a single stored value when it is called instead of iterating over the entire table.