| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 | [discrete][[esql-inlinestats-by]]=== `INLINESTATS ... BY`experimental::["INLINESTATS is highly experimental and only available in SNAPSHOT versions."]The `INLINESTATS` command calculates an aggregate result and adds new columnswith the result to the stream of input data.**Syntax**[source,esql]----INLINESTATS [column1 =] expression1[, ..., [columnN =] expressionN][BY grouping_expression1[, ..., grouping_expressionN]]----*Parameters*`columnX`::The name by which the aggregated value is returned. If omitted, the name isequal to the corresponding expression (`expressionX`). If multiple columnshave the same name, all but the rightmost column with this name will be ignored.`expressionX`::An expression that computes an aggregated value. If its name coincides with oneof the computed columns, that column will be ignored.`grouping_expressionX`::An expression that outputs the values to group by.NOTE: Individual `null` values are skipped when computing aggregations.*Description*The `INLINESTATS` command calculates an aggregate result and merges that resultback into the stream of input data. Without the optional `BY` clause this willproduce a single result which is appended to each row. With a `BY` clause thiswill produce one result per grouping and merge the result into the stream based onmatching group keys.All of the <<esql-agg-functions,aggregation functions>> are supported.*Examples*Find the employees that speak the most languages (it's a tie!):[source.merge.styled,esql]----include::{esql-specs}/inlinestats.csv-spec[tag=max-languages]----[%header.monospaced.styled,format=dsv,separator=|]|===include::{esql-specs}/inlinestats.csv-spec[tag=max-languages-result]|===Find the longest tenured employee who's last name starts with each letter of the alphabet:[source.merge.styled,esql]----include::{esql-specs}/inlinestats.csv-spec[tag=longest-tenured-by-first]----[%header.monospaced.styled,format=dsv,separator=|]|===include::{esql-specs}/inlinestats.csv-spec[tag=longest-tenured-by-first-result]|===Find the northern and southern most airports:[source.merge.styled,esql]----include::{esql-specs}/inlinestats.csv-spec[tag=extreme-airports]----[%header.monospaced.styled,format=dsv,separator=|]|===include::{esql-specs}/inlinestats.csv-spec[tag=extreme-airports-result]|===NOTE: Our test data doesn't have many "small" airports.If a `BY` field is multivalued then `INLINESTATS` will put the row in *each*bucket like <<esql-stats-by>>:[source.merge.styled,esql]----include::{esql-specs}/inlinestats.csv-spec[tag=mv-group]----[%header.monospaced.styled,format=dsv,separator=|]|===include::{esql-specs}/inlinestats.csv-spec[tag=mv-group-result]|===To treat each group key as its own row use <<esql-mv_expand>> before `INLINESTATS`:[source.merge.styled,esql]----include::{esql-specs}/inlinestats.csv-spec[tag=mv-expand]----[%header.monospaced.styled,format=dsv,separator=|]|===include::{esql-specs}/inlinestats.csv-spec[tag=mv-expand-result]|===
 |