top() function

The top() function sorts a table by columns and keeps only the top n records.

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

  1. top(n:10, columns: ["_value"])

Empty tables

top() drops empty tables.

Parameters

n

Number of records to return.

*Data type: Integer*

columns

List of columns by which to sort. Sort precedence is determined by list order (left to right). Default is ["_value"].

*Data type: Array of strings*

Examples

  1. from(bucket:"example-bucket")
  2. |> range(start:-1h)
  3. |> filter(fn: (r) =>
  4. r._measurement == "mem" and
  5. r._field == "used_percent"
  6. )
  7. |> top(n:10)

Function definition

  1. // _sortLimit is a helper function, which sorts and limits a table.
  2. _sortLimit = (n, desc, columns=["_value"], tables=<-) =>
  3. tables
  4. |> sort(columns:columns, desc:desc)
  5. |> limit(n:n)
  6. top = (n, columns=["_value"], tables=<-) => _sortLimit(n:n, columns:columns, desc:true)