stats.asciidoc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. [discrete]
  2. [[esql-stats-by]]
  3. === `STATS ... BY`
  4. The `STATS ... BY` processing command groups rows according to a common value
  5. and calculate one or more aggregated values over the grouped rows.
  6. **Syntax**
  7. [source,esql]
  8. ----
  9. STATS [column1 =] expression1[, ..., [columnN =] expressionN]
  10. [BY grouping_expression1[, ..., grouping_expressionN]]
  11. ----
  12. *Parameters*
  13. `columnX`::
  14. The name by which the aggregated value is returned. If omitted, the name is
  15. equal to the corresponding expression (`expressionX`).
  16. `expressionX`::
  17. An expression that computes an aggregated value.
  18. `grouping_expressionX`::
  19. An expression that outputs the values to group by.
  20. NOTE: Individual `null` values are skipped when computing aggregations.
  21. *Description*
  22. The `STATS ... BY` processing command groups rows according to a common value
  23. and calculate one or more aggregated values over the grouped rows. If `BY` is
  24. omitted, the output table contains exactly one row with the aggregations applied
  25. over the entire dataset.
  26. The following <<esql-agg-functions,aggregation functions>> are supported:
  27. include::../functions/aggregation-functions.asciidoc[tag=agg_list]
  28. NOTE: `STATS` without any groups is much much faster than adding a group.
  29. NOTE: Grouping on a single expression is currently much more optimized than grouping
  30. on many expressions. In some tests we have seen grouping on a single `keyword`
  31. column to be five times faster than grouping on two `keyword` columns. Do
  32. not try to work around this by combining the two columns together with
  33. something like <<esql-concat>> and then grouping - that is not going to be
  34. faster.
  35. *Examples*
  36. Calculating a statistic and grouping by the values of another column:
  37. [source.merge.styled,esql]
  38. ----
  39. include::{esql-specs}/stats.csv-spec[tag=stats]
  40. ----
  41. [%header.monospaced.styled,format=dsv,separator=|]
  42. |===
  43. include::{esql-specs}/stats.csv-spec[tag=stats-result]
  44. |===
  45. Omitting `BY` returns one row with the aggregations applied over the entire
  46. dataset:
  47. [source.merge.styled,esql]
  48. ----
  49. include::{esql-specs}/stats.csv-spec[tag=statsWithoutBy]
  50. ----
  51. [%header.monospaced.styled,format=dsv,separator=|]
  52. |===
  53. include::{esql-specs}/stats.csv-spec[tag=statsWithoutBy-result]
  54. |===
  55. It's possible to calculate multiple values:
  56. [source.merge.styled,esql]
  57. ----
  58. include::{esql-specs}/stats.csv-spec[tag=statsCalcMultipleValues]
  59. ----
  60. [%header.monospaced.styled,format=dsv,separator=|]
  61. |===
  62. include::{esql-specs}/stats.csv-spec[tag=statsCalcMultipleValues-result]
  63. |===
  64. [[esql-stats-mv-group]]
  65. If the grouping key is multivalued then the input row is in all groups:
  66. [source.merge.styled,esql]
  67. ----
  68. include::{esql-specs}/stats.csv-spec[tag=mv-group]
  69. ----
  70. [%header.monospaced.styled,format=dsv,separator=|]
  71. |===
  72. include::{esql-specs}/stats.csv-spec[tag=mv-group-result]
  73. |===
  74. It's also possible to group by multiple values:
  75. [source,esql]
  76. ----
  77. include::{esql-specs}/stats.csv-spec[tag=statsGroupByMultipleValues]
  78. ----
  79. If the all grouping keys are multivalued then the input row is in all groups:
  80. [source.merge.styled,esql]
  81. ----
  82. include::{esql-specs}/stats.csv-spec[tag=multi-mv-group]
  83. ----
  84. [%header.monospaced.styled,format=dsv,separator=|]
  85. |===
  86. include::{esql-specs}/stats.csv-spec[tag=multi-mv-group-result]
  87. |===
  88. Both the aggregating functions and the grouping expressions accept other
  89. functions. This is useful for using `STATS...BY` on multivalue columns.
  90. For example, to calculate the average salary change, you can use `MV_AVG` to
  91. first average the multiple values per employee, and use the result with the
  92. `AVG` function:
  93. [source.merge.styled,esql]
  94. ----
  95. include::{esql-specs}/stats.csv-spec[tag=docsStatsAvgNestedExpression]
  96. ----
  97. [%header.monospaced.styled,format=dsv,separator=|]
  98. |===
  99. include::{esql-specs}/stats.csv-spec[tag=docsStatsAvgNestedExpression-result]
  100. |===
  101. An example of grouping by an expression is grouping employees on the first
  102. letter of their last name:
  103. [source.merge.styled,esql]
  104. ----
  105. include::{esql-specs}/stats.csv-spec[tag=docsStatsByExpression]
  106. ----
  107. [%header.monospaced.styled,format=dsv,separator=|]
  108. |===
  109. include::{esql-specs}/stats.csv-spec[tag=docsStatsByExpression-result]
  110. |===
  111. Specifying the output column name is optional. If not specified, the new column
  112. name is equal to the expression. The following query returns a column named
  113. `AVG(salary)`:
  114. [source.merge.styled,esql]
  115. ----
  116. include::{esql-specs}/stats.csv-spec[tag=statsUnnamedColumn]
  117. ----
  118. [%header.monospaced.styled,format=dsv,separator=|]
  119. |===
  120. include::{esql-specs}/stats.csv-spec[tag=statsUnnamedColumn-result]
  121. |===
  122. Because this name contains special characters, <<esql-identifiers,it needs to be
  123. quoted>> with backticks (+{backtick}+) when using it in subsequent commands:
  124. [source.merge.styled,esql]
  125. ----
  126. include::{esql-specs}/stats.csv-spec[tag=statsUnnamedColumnEval]
  127. ----
  128. [%header.monospaced.styled,format=dsv,separator=|]
  129. |===
  130. include::{esql-specs}/stats.csv-spec[tag=statsUnnamedColumnEval-result]
  131. |===