stats.asciidoc 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. * `AVG`
  36. * `COUNT`
  37. * `COUNT_DISTINCT`
  38. * `MAX`
  39. * `MEDIAN`
  40. * `MEDIAN_ABSOLUTE_DEVIATION`
  41. * `MIN`
  42. * `SUM`