count.asciidoc 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. [discrete]
  2. [[esql-agg-count]]
  3. === `COUNT`
  4. *Syntax*
  5. [source,esql]
  6. ----
  7. COUNT([expression])
  8. ----
  9. *Parameters*
  10. `expression`::
  11. Expression that outputs values to be counted.
  12. If omitted, equivalent to `COUNT(*)` (the number of rows).
  13. *Description*
  14. Returns the total number (count) of input values.
  15. *Supported types*
  16. Can take any field type as input.
  17. *Examples*
  18. [source.merge.styled,esql]
  19. ----
  20. include::{esql-specs}/stats.csv-spec[tag=count]
  21. ----
  22. [%header.monospaced.styled,format=dsv,separator=|]
  23. |===
  24. include::{esql-specs}/stats.csv-spec[tag=count-result]
  25. |===
  26. To count the number of rows, use `COUNT()` or `COUNT(*)`:
  27. [source.merge.styled,esql]
  28. ----
  29. include::{esql-specs}/docs.csv-spec[tag=countAll]
  30. ----
  31. [%header.monospaced.styled,format=dsv,separator=|]
  32. |===
  33. include::{esql-specs}/docs.csv-spec[tag=countAll-result]
  34. |===
  35. The expression can use inline functions. This example splits a string into
  36. multiple values using the `SPLIT` function and counts the values:
  37. [source.merge.styled,esql]
  38. ----
  39. include::{esql-specs}/stats.csv-spec[tag=docsCountWithExpression]
  40. ----
  41. [%header.monospaced.styled,format=dsv,separator=|]
  42. |===
  43. include::{esql-specs}/stats.csv-spec[tag=docsCountWithExpression-result]
  44. |===
  45. [[esql-agg-count-or-null]]
  46. To count the number of times an expression returns `TRUE` use
  47. a <<esql-where>> command to remove rows that shouldn't be included:
  48. [source.merge.styled,esql]
  49. ----
  50. include::{esql-specs}/stats.csv-spec[tag=count-where]
  51. ----
  52. [%header.monospaced.styled,format=dsv,separator=|]
  53. |===
  54. include::{esql-specs}/stats.csv-spec[tag=count-where-result]
  55. |===
  56. To count the same stream of data based on two different expressions
  57. use the pattern `COUNT(<expression> OR NULL)`:
  58. [source.merge.styled,esql]
  59. ----
  60. include::{esql-specs}/stats.csv-spec[tag=count-or-null]
  61. ----
  62. [%header.monospaced.styled,format=dsv,separator=|]
  63. |===
  64. include::{esql-specs}/stats.csv-spec[tag=count-or-null-result]
  65. |===