count.asciidoc 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // This is generated by ESQL's AbstractFunctionTestCase. Do no edit it. See ../README.md for how to regenerate it.
  2. *Examples*
  3. [source.merge.styled,esql]
  4. ----
  5. include::{esql-specs}/stats.csv-spec[tag=count]
  6. ----
  7. [%header.monospaced.styled,format=dsv,separator=|]
  8. |===
  9. include::{esql-specs}/stats.csv-spec[tag=count-result]
  10. |===
  11. To count the number of rows, use `COUNT()` or `COUNT(*)`
  12. [source.merge.styled,esql]
  13. ----
  14. include::{esql-specs}/docs.csv-spec[tag=countAll]
  15. ----
  16. [%header.monospaced.styled,format=dsv,separator=|]
  17. |===
  18. include::{esql-specs}/docs.csv-spec[tag=countAll-result]
  19. |===
  20. The expression can use inline functions. This example splits a string into multiple values using the `SPLIT` function and counts the values
  21. [source.merge.styled,esql]
  22. ----
  23. include::{esql-specs}/stats.csv-spec[tag=docsCountWithExpression]
  24. ----
  25. [%header.monospaced.styled,format=dsv,separator=|]
  26. |===
  27. include::{esql-specs}/stats.csv-spec[tag=docsCountWithExpression-result]
  28. |===
  29. To count the number of times an expression returns `TRUE` use a <<esql-where>> command to remove rows that shouldn't be included
  30. [source.merge.styled,esql]
  31. ----
  32. include::{esql-specs}/stats.csv-spec[tag=count-where]
  33. ----
  34. [%header.monospaced.styled,format=dsv,separator=|]
  35. |===
  36. include::{esql-specs}/stats.csv-spec[tag=count-where-result]
  37. |===
  38. To count the same stream of data based on two different expressions use the pattern `COUNT(<expression> OR NULL)`. This builds on the three-valued logic ({wikipedia}/Three-valued_logic[3VL]) of the language: `TRUE OR NULL` is `TRUE`, but `FALSE OR NULL` is `NULL`, plus the way COUNT handles `NULL`s: `COUNT(TRUE)` and `COUNT(FALSE)` are both 1, but `COUNT(NULL)` is 0.
  39. [source.merge.styled,esql]
  40. ----
  41. include::{esql-specs}/stats.csv-spec[tag=count-or-null]
  42. ----
  43. [%header.monospaced.styled,format=dsv,separator=|]
  44. |===
  45. include::{esql-specs}/stats.csv-spec[tag=count-or-null-result]
  46. |===