Documentation

pivot() function

pivot() collects unique values stored vertically (column-wise) and aligns them horizontally (row-wise) into logical sets.

Output data

The group key of the resulting table is the same as the input tables, excluding columns found in the columnKey and valueColumn parameters. These columns are not part of the resulting output table and are dropped from the group key.

Every input row should have a 1:1 mapping to a particular row and column combination in the output table. Row and column combinations are determined by the rowKey and columnKey parameters. In cases where more than one value is identified for the same row and column pair, the last value encountered in the set of table rows is used as the result.

The output is constructed as follows:

  • The set of columns for the new table is the rowKey unioned with the group key, but excluding the columns indicated by the columnKey and the valueColumn.
  • A new column is added to the set of columns for each unique value identified by the columnKey parameter.
  • The label of a new column is the concatenation of the values of columnKey using _ as a separator. If the value is null, “null” is used.
  • A new row is created for each unique value identified by the rowKey parameter.
  • For each new row, values for group key columns stay the same, while values for new columns are determined from the input tables by the value in valueColumn at the row identified by the rowKey values and the new column’s label. If no value is found, the value is set to null.
  • Any column that is not part of the group key or not specified in the rowKey, columnKey, and valueColumn parameters is dropped.
Function type signature
(<-tables: stream[A], columnKey: [string], rowKey: [string], valueColumn: string) => stream[B] where A: Record, B: Record
For more information, see Function type signatures.

Parameters

rowKey

(Required) Columns to use to uniquely identify an output row.

columnKey

(Required) Columns to use to identify new output columns.

valueColumn

(Required) Column to use to populate the value of pivoted columnKey columns.

tables

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

Examples

Align fields into rows based on time

data
    |> pivot(rowKey: ["_time"], columnKey: ["_field"], valueColumn: "_value")

View example input and output

Associate values to tags by time

import "sampledata"

sampledata.int()
    |> pivot(rowKey: ["_time"], columnKey: ["tag"], valueColumn: "_value")

View example input and output


Was this page helpful?

Thank you for your feedback!


Introducing InfluxDB Clustered

A highly available InfluxDB 3.0 cluster on your own infrastructure.

InfluxDB Clustered is a highly available InfluxDB 3.0 cluster built for high write and query workloads on your own infrastructure.

InfluxDB Clustered is currently in limited availability and is only available to a limited group of InfluxData customers. If interested in being part of the limited access group, please contact the InfluxData Sales team.

Learn more
Contact InfluxData Sales

The future of Flux

Flux is going into maintenance mode. You can continue using it as you currently are without any changes to your code.

Flux is going into maintenance mode and will not be supported in InfluxDB 3.0. This was a decision based on the broad demand for SQL and the continued growth and adoption of InfluxQL. We are continuing to support Flux for users in 1.x and 2.x so you can continue using it with no changes to your code. If you are interested in transitioning to InfluxDB 3.0 and want to future-proof your code, we suggest using InfluxQL.

For information about the future of Flux, see the following:

State of the InfluxDB Cloud Serverless documentation

InfluxDB Cloud Serverless documentation is a work in progress.

The new documentation for InfluxDB Cloud Serverless is a work in progress. We are adding new information and content almost daily. Thank you for your patience!

If there is specific information you’re looking for, please submit a documentation issue.