stats.asciidoc 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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,esql]
  6. ----
  7. include::{esql-specs}/docs.csv-spec[tag=stats]
  8. ----
  9. Which returns:
  10. [%header,format=dsv,separator=|]
  11. |===
  12. include::{esql-specs}/docs.csv-spec[tag=stats-result]
  13. |===
  14. If `BY` is omitted, the output table contains exactly one row with the
  15. aggregations applied over the entire dataset:
  16. [source,esql]
  17. ----
  18. include::{esql-specs}/docs.csv-spec[tag=statsWithoutBy]
  19. ----
  20. Returning:
  21. [%header,format=dsv,separator=|]
  22. |===
  23. include::{esql-specs}/docs.csv-spec[tag=statsWithoutBy-result]
  24. |===
  25. It's possible to calculate multiple values:
  26. [source,esql]
  27. ----
  28. include::{esql-specs}/docs.csv-spec[tag=statsCalcMultipleValues]
  29. ----
  30. It's also possible to group by multiple values (only supported for long and
  31. keyword family fields):
  32. [source,esql]
  33. ----
  34. include::{esql-specs}/docs.csv-spec[tag=statsGroupByMultipleValues]
  35. ----
  36. The following aggregation functions are supported:
  37. * `AVG`
  38. * `COUNT`
  39. * `COUNT_DISTINCT`
  40. * `MAX`
  41. * `MEDIAN`
  42. * `MEDIAN_ABSOLUTE_DEVIATION`
  43. * `MIN`
  44. * `SUM`