stats.asciidoc 5.0 KB

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