median.asciidoc 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. [discrete]
  2. [[esql-agg-median]]
  3. === `MEDIAN`
  4. *Syntax*
  5. [source,esql]
  6. ----
  7. MEDIAN(expression)
  8. ----
  9. *Parameters*
  10. `expression`::
  11. Expression from which to return the median value.
  12. *Description*
  13. Returns the value that is greater than half of all values and less than half of
  14. all values, also known as the 50% <<esql-agg-percentile>>.
  15. NOTE: Like <<esql-agg-percentile>>, `MEDIAN` is <<esql-agg-percentile-approximate,usually approximate>>.
  16. [WARNING]
  17. ====
  18. `MEDIAN` is also {wikipedia}/Nondeterministic_algorithm[non-deterministic].
  19. This means you can get slightly different results using the same data.
  20. ====
  21. *Example*
  22. [source.merge.styled,esql]
  23. ----
  24. include::{esql-specs}/stats_percentile.csv-spec[tag=median]
  25. ----
  26. [%header.monospaced.styled,format=dsv,separator=|]
  27. |===
  28. include::{esql-specs}/stats_percentile.csv-spec[tag=median-result]
  29. |===
  30. The expression can use inline functions. For example, to calculate the median of
  31. the maximum values of a multivalued column, first use `MV_MAX` to get the
  32. maximum value per row, and use the result with the `MEDIAN` function:
  33. [source.merge.styled,esql]
  34. ----
  35. include::{esql-specs}/stats_percentile.csv-spec[tag=docsStatsMedianNestedExpression]
  36. ----
  37. [%header.monospaced.styled,format=dsv,separator=|]
  38. |===
  39. include::{esql-specs}/stats_percentile.csv-spec[tag=docsStatsMedianNestedExpression-result]
  40. |===