median() function

The median() function is a special application of the quantile() function that returns the median _value of an input table or all non-null records in the input table with values that fall within the 0.5 quantile (50th percentile) depending on the method used.

*Function type: Selector or Aggregate
**
Output data type:* Record

  1. median(
  2. column: "_value",
  3. method: "estimate_tdigest",
  4. compression: 0.0
  5. )

When using the estimate_tdigest or exact_mean methods, it outputs non-null records with values that fall within the 0.5 quantile.

When using the exact_selector method, it outputs the non-null record with the value that represents the 0.5 quantile.

The median() function can only be used with float value types. It is a special application of the quantile() function which uses an approximation implementation that requires floats. You can convert your value column to a float column using the toFloat() function.

Parameters

column

The column to use to compute the median. Defaults to "_value".

*Data type: String*

method

Defines the method of computation. Defaults to "estimate_tdigest".

*Data type: String*

The available options are:

estimate_tdigest

An aggregate method that uses a t-digest data structure to compute an accurate quantile estimate on large data sources.

exact_mean

An aggregate method that takes the average of the two points closest to the quantile value.

exact_selector

A selector method that returns the data point for which at least q points are less than.

compression

Indicates how many centroids to use when compressing the dataset. A larger number produces a more accurate result at the cost of increased memory requirements. Defaults to 1000.0.

*Data type: Float*

Examples

Median as an aggregate
  1. from(bucket: "example-bucket")
  2. |> filter(fn: (r) =>
  3. r._measurement == "mem" and
  4. r._field == "used_percent"
  5. )
  6. |> range(start:-12h)
  7. |> window(every:10m)
  8. |> median()
Median as a selector
  1. from(bucket: "example-bucket")
  2. |> filter(fn: (r) =>
  3. r._measurement == "mem" and
  4. r._field == "used_percent"
  5. )
  6. |> range(start:-12h)
  7. |> window(every:10m)
  8. |> median(method: "exact_selector")

Function definition

  1. median = (method="estimate_tdigest", compression=0.0, tables=<-) =>
  2. quantile(
  3. q:0.5,
  4. method:method,
  5. compression:compression
  6. )

Related articles