docs-v2/content/flux/v0.x/stdlib/universe/top.md

2.1 KiB

title description aliases menu weight flux/v0.x/tags introduced
top() function The `top()` function sorts a table by columns and keeps only the top n records.
/influxdb/v2.0/reference/flux/functions/transformations/selectors/top
/influxdb/v2.0/reference/flux/functions/built-in/transformations/selectors/top/
/influxdb/v2.0/reference/flux/stdlib/built-in/transformations/selectors/top/
/influxdb/cloud/reference/flux/stdlib/built-in/transformations/selectors/top/
flux_0_x_ref
name parent
top universe
102
selectors
transformations
0.7.0

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

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

{{% warn %}}

Empty tables

top() drops empty tables. {{% /warn %}}

Parameters

n

({{< req >}}) Number of records to return.

columns

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

tables

Input data. Default is piped-forward data (<-).

Examples

{{% flux/sample-example-intro %}}

Return rows with the top three values in each input table
import "sampledata"

sampledata.int()
    |> top(n: 3)

{{< expand-wrapper >}} {{% expand "View input and output" %}} {{< flex >}} {{% flex-content %}}

Input data

{{% flux/sample "float" %}}

{{% /flex-content %}} {{% flex-content %}}

Output data
_time tag _value
2021-01-01T00:00:30Z t1 17
2021-01-01T00:00:40Z t1 15
2021-01-01T00:00:10Z t1 10
_time tag _value
2021-01-01T00:00:00Z t2 19
2021-01-01T00:00:30Z t2 19
2021-01-01T00:00:40Z t2 13
{{% /flex-content %}}
{{< /flex >}}
{{% /expand %}}
{{< /expand-wrapper >}}