stats.asciidoc 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. [discrete]
  2. [[esql-stats-by]]
  3. === `STATS ... BY`
  4. Use `STATS ... BY` to group rows according to a common value and calculate one
  5. or more aggregated values over the grouped rows.
  6. [source.merge.styled,esql]
  7. ----
  8. include::{esql-specs}/docs.csv-spec[tag=stats]
  9. ----
  10. [%header.monospaced.styled,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.merge.styled,esql]
  17. ----
  18. include::{esql-specs}/docs.csv-spec[tag=statsWithoutBy]
  19. ----
  20. [%header.monospaced.styled,format=dsv,separator=|]
  21. |===
  22. include::{esql-specs}/docs.csv-spec[tag=statsWithoutBy-result]
  23. |===
  24. It's possible to calculate multiple values:
  25. [source,esql]
  26. ----
  27. include::{esql-specs}/docs.csv-spec[tag=statsCalcMultipleValues]
  28. ----
  29. It's also possible to group by multiple values (only supported for long and
  30. keyword family fields):
  31. [source,esql]
  32. ----
  33. include::{esql-specs}/docs.csv-spec[tag=statsGroupByMultipleValues]
  34. ----
  35. The following aggregation functions are supported:
  36. include::../functions/aggregation-functions.asciidoc[tag=agg_list]
  37. NOTE: `STATS` without any groups is much much faster than adding group.
  38. NOTE: Grouping on a single field is currently much more optimized than grouping
  39. on many fields. In some tests we've seen grouping on a single `keyword`
  40. field to be five times faster than grouping on two `keyword` fields. Don't
  41. try to work around this combining the two fields together with something
  42. like <<esql-concat>> and then grouping - that's not going to be faster.