Skip to content

Commit

Permalink
ESQL: Document the pattern to count TRUE
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
nik9000 committed Jul 12, 2024
1 parent e1a091f commit aeacae6
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
13 changes: 13 additions & 0 deletions docs/reference/esql/functions/count.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,16 @@ include::{esql-specs}/stats.csv-spec[tag=docsCountWithExpression]
|===
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
`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]
|===
Original file line number Diff line number Diff line change
Expand Up @@ -1158,6 +1158,19 @@ word_count:long
// end::docsCountWithExpression-result[]
;

count_or_null
// tag::count-or-null[]
ROW n=1
| STATS COUNT(n > 0 OR NULL), COUNT(n < 0 OR NULL)
// end::count-or-null[]
;

// tag::count-or-null-result[]
COUNT(n > 0 OR NULL):long | COUNT(n < 0 OR NULL):long
1 | 0
// end::count-or-null-result[]
;

countMultiValuesRow
ROW keyword_field = ["foo", "bar"], int_field = [1, 2, 3] | STATS ck = COUNT(keyword_field), ci = COUNT(int_field), c = COUNT(*);

Expand Down

0 comments on commit aeacae6

Please sign in to comment.