mirror of
https://github.com/elastic/elasticsearch.git
synced 2025-06-30 18:33:26 -04:00
This adds an example to the docs an example of counting the TRUE results of an expression. You do `COUNT(a > 0 OR NULL)`. That turns the `FALSE` into `NULL`. Which you need to do because `COUNT(false)` is `1` - because it's a value. But `COUNT(null)` is `0` - because it's the absence of values. We could like to make something more intuitive for this one day. But for now, this is what works.
83 lines
1.9 KiB
Text
83 lines
1.9 KiB
Text
[discrete]
|
|
[[esql-agg-count]]
|
|
=== `COUNT`
|
|
|
|
*Syntax*
|
|
|
|
[source,esql]
|
|
----
|
|
COUNT([expression])
|
|
----
|
|
|
|
*Parameters*
|
|
|
|
`expression`::
|
|
Expression that outputs values to be counted.
|
|
If omitted, equivalent to `COUNT(*)` (the number of rows).
|
|
|
|
*Description*
|
|
|
|
Returns the total number (count) of input values.
|
|
|
|
*Supported types*
|
|
|
|
Can take any field type as input.
|
|
|
|
*Examples*
|
|
|
|
[source.merge.styled,esql]
|
|
----
|
|
include::{esql-specs}/stats.csv-spec[tag=count]
|
|
----
|
|
[%header.monospaced.styled,format=dsv,separator=|]
|
|
|===
|
|
include::{esql-specs}/stats.csv-spec[tag=count-result]
|
|
|===
|
|
|
|
To count the number of rows, use `COUNT()` or `COUNT(*)`:
|
|
|
|
[source.merge.styled,esql]
|
|
----
|
|
include::{esql-specs}/docs.csv-spec[tag=countAll]
|
|
----
|
|
[%header.monospaced.styled,format=dsv,separator=|]
|
|
|===
|
|
include::{esql-specs}/docs.csv-spec[tag=countAll-result]
|
|
|===
|
|
|
|
The expression can use inline functions. This example splits a string into
|
|
multiple values using the `SPLIT` function and counts the values:
|
|
|
|
[source.merge.styled,esql]
|
|
----
|
|
include::{esql-specs}/stats.csv-spec[tag=docsCountWithExpression]
|
|
----
|
|
[%header.monospaced.styled,format=dsv,separator=|]
|
|
|===
|
|
include::{esql-specs}/stats.csv-spec[tag=docsCountWithExpression-result]
|
|
|===
|
|
|
|
[[esql-agg-count-or-null]]
|
|
To count the number of times an expression returns `TRUE` use
|
|
a <<esql-where>> command to remove rows that shouldn't be included:
|
|
|
|
[source.merge.styled,esql]
|
|
----
|
|
include::{esql-specs}/stats.csv-spec[tag=count-where]
|
|
----
|
|
[%header.monospaced.styled,format=dsv,separator=|]
|
|
|===
|
|
include::{esql-specs}/stats.csv-spec[tag=count-where-result]
|
|
|===
|
|
|
|
To count the same stream of data based on two different expressions
|
|
use the pattern `COUNT(<expression> OR NULL)`:
|
|
|
|
[source.merge.styled,esql]
|
|
----
|
|
include::{esql-specs}/stats.csv-spec[tag=count-or-null]
|
|
----
|
|
[%header.monospaced.styled,format=dsv,separator=|]
|
|
|===
|
|
include::{esql-specs}/stats.csv-spec[tag=count-or-null-result]
|
|
|===
|