stats.asciidoc 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. [[esql-stats-by]]
  2. === `STATS ... BY`
  3. Use `STATS ... BY` to group rows according to a common value and calculate one
  4. or more aggregated values over the grouped rows.
  5. [source.merge.styled,esql]
  6. ----
  7. include::{esql-specs}/docs.csv-spec[tag=stats]
  8. ----
  9. [%header.monospaced.styled,format=dsv,separator=|]
  10. |===
  11. include::{esql-specs}/docs.csv-spec[tag=stats-result]
  12. |===
  13. If `BY` is omitted, the output table contains exactly one row with the
  14. aggregations applied over the entire dataset:
  15. [source.merge.styled,esql]
  16. ----
  17. include::{esql-specs}/docs.csv-spec[tag=statsWithoutBy]
  18. ----
  19. [%header.monospaced.styled,format=dsv,separator=|]
  20. |===
  21. include::{esql-specs}/docs.csv-spec[tag=statsWithoutBy-result]
  22. |===
  23. It's possible to calculate multiple values:
  24. [source,esql]
  25. ----
  26. include::{esql-specs}/docs.csv-spec[tag=statsCalcMultipleValues]
  27. ----
  28. It's also possible to group by multiple values (only supported for long and
  29. keyword family fields):
  30. [source,esql]
  31. ----
  32. include::{esql-specs}/docs.csv-spec[tag=statsGroupByMultipleValues]
  33. ----
  34. The following aggregation functions are supported:
  35. include::../aggregation-functions.asciidoc[tag=functions]