inlinestats.asciidoc 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. [discrete]
  2. [[esql-inlinestats-by]]
  3. === `INLINESTATS ... BY`
  4. experimental::["INLINESTATS is highly experimental and only available in SNAPSHOT versions."]
  5. The `INLINESTATS` command calculates an aggregate result and adds new columns
  6. with the result to the stream of input data.
  7. **Syntax**
  8. [source,esql]
  9. ----
  10. INLINESTATS [column1 =] expression1[, ..., [columnN =] expressionN]
  11. [BY grouping_expression1[, ..., grouping_expressionN]]
  12. ----
  13. *Parameters*
  14. `columnX`::
  15. The name by which the aggregated value is returned. If omitted, the name is
  16. equal to the corresponding expression (`expressionX`). If multiple columns
  17. have the same name, all but the rightmost column with this name will be ignored.
  18. `expressionX`::
  19. An expression that computes an aggregated value. If its name coincides with one
  20. of the computed columns, that column will be ignored.
  21. `grouping_expressionX`::
  22. An expression that outputs the values to group by.
  23. NOTE: Individual `null` values are skipped when computing aggregations.
  24. *Description*
  25. The `INLINESTATS` command calculates an aggregate result and merges that result
  26. back into the stream of input data. Without the optional `BY` clause this will
  27. produce a single result which is appended to each row. With a `BY` clause this
  28. will produce one result per grouping and merge the result into the stream based on
  29. matching group keys.
  30. All of the <<esql-agg-functions,aggregation functions>> are supported.
  31. *Examples*
  32. Find the employees that speak the most languages (it's a tie!):
  33. [source.merge.styled,esql]
  34. ----
  35. include::{esql-specs}/inlinestats.csv-spec[tag=max-languages]
  36. ----
  37. [%header.monospaced.styled,format=dsv,separator=|]
  38. |===
  39. include::{esql-specs}/inlinestats.csv-spec[tag=max-languages-result]
  40. |===
  41. Find the longest tenured employee who's last name starts with each letter of the alphabet:
  42. [source.merge.styled,esql]
  43. ----
  44. include::{esql-specs}/inlinestats.csv-spec[tag=longest-tenured-by-first]
  45. ----
  46. [%header.monospaced.styled,format=dsv,separator=|]
  47. |===
  48. include::{esql-specs}/inlinestats.csv-spec[tag=longest-tenured-by-first-result]
  49. |===
  50. Find the northern and southern most airports:
  51. [source.merge.styled,esql]
  52. ----
  53. include::{esql-specs}/inlinestats.csv-spec[tag=extreme-airports]
  54. ----
  55. [%header.monospaced.styled,format=dsv,separator=|]
  56. |===
  57. include::{esql-specs}/inlinestats.csv-spec[tag=extreme-airports-result]
  58. |===
  59. NOTE: Our test data doesn't have many "small" airports.
  60. If a `BY` field is multivalued then `INLINESTATS` will put the row in *each*
  61. bucket like <<esql-stats-by>>:
  62. [source.merge.styled,esql]
  63. ----
  64. include::{esql-specs}/inlinestats.csv-spec[tag=mv-group]
  65. ----
  66. [%header.monospaced.styled,format=dsv,separator=|]
  67. |===
  68. include::{esql-specs}/inlinestats.csv-spec[tag=mv-group-result]
  69. |===
  70. To treat each group key as its own row use <<esql-mv_expand>> before `INLINESTATS`:
  71. [source.merge.styled,esql]
  72. ----
  73. include::{esql-specs}/inlinestats.csv-spec[tag=mv-expand]
  74. ----
  75. [%header.monospaced.styled,format=dsv,separator=|]
  76. |===
  77. include::{esql-specs}/inlinestats.csv-spec[tag=mv-expand-result]
  78. |===