pivot() function

The pivot() function collects values stored vertically (column-wise) in a table and aligns them horizontally (row-wise) into logical sets.

*Function type: Transformation
**
Output data type:* Record

  1. pivot(rowKey:["_time"], columnKey: ["_field"], valueColumn: "_value")

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

Every input row should have a 1:1 mapping to a particular row + column in the output table, determined by its values for the rowKey and columnKey parameters. In cases where more than one value is identified for the same row + 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 in the input 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 in the input 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.

Parameters

rowKey

List of columns used to uniquely identify a row for the output.

*Data type: Array of strings*

columnKey

List of columns used to pivot values onto each row identified by the rowKey.

*Data type: Array of strings*

valueColumn

The single column that contains the value to be moved around the pivot.

*Data type: String*

Examples

Align fields within each measurement that have the same timestamp

  1. from(bucket:"test")
  2. |> range(start: 1970-01-01T00:00:00.000000000Z)
  3. |> pivot(
  4. rowKey:["_time"],
  5. columnKey: ["_field"],
  6. valueColumn: "_value"
  7. )
Input
_time_value_measurement_field
1970-01-01T00:00:00.000000001Z1.0“m1”“f1”
1970-01-01T00:00:00.000000001Z2.0“m1”“f2”
1970-01-01T00:00:00.000000001Znull“m1”“f3”
1970-01-01T00:00:00.000000001Z3.0“m1”null
1970-01-01T00:00:00.000000002Z4.0“m1”“f1”
1970-01-01T00:00:00.000000002Z5.0“m1”“f2”
null6.0“m1”“f2”
1970-01-01T00:00:00.000000002Znull“m1”“f3”
1970-01-01T00:00:00.000000003Znull“m1”“f1”
1970-01-01T00:00:00.000000003Z7.0“m1”null
1970-01-01T00:00:00.000000004Z8.0“m1”“f3”
Output
_time_measurementf1f2f3null
1970-01-01T00:00:00.000000001Z“m1”1.02.0null3.0
1970-01-01T00:00:00.000000002Z“m1”4.05.0nullnull
null“m1”null6.0nullnull
1970-01-01T00:00:00.000000003Z“m1”nullnullnull7.0
1970-01-01T00:00:00.000000004Z“m1”nullnull8.0null

Align fields and measurements that have the same timestamp

Note the effects of:

  • Having null values in some columnKey value;
  • Having more values for the same rowKey and columnKey value (the 11th row overrides the 10th, and so does the 15th with the 14th).
  1. from(bucket:"test")
  2. |> range(start: 1970-01-01T00:00:00.000000000Z)
  3. |> pivot(
  4. rowKey:["_time"],
  5. columnKey: ["_measurement", "_field"],
  6. valueColumn: "_value"
  7. )
Input
_time_value_measurement_field
1970-01-01T00:00:00.000000001Z1.0“m1”“f1”
1970-01-01T00:00:00.000000001Z2.0“m1”“f2”
1970-01-01T00:00:00.000000001Z3.0null“f3”
1970-01-01T00:00:00.000000001Z4.0nullnull
1970-01-01T00:00:00.000000002Z5.0“m1”“f1”
1970-01-01T00:00:00.000000002Z6.0“m1”“f2”
1970-01-01T00:00:00.000000002Z7.0“m1”“f3”
1970-01-01T00:00:00.000000002Z8.0nullnull
null9.0“m1”“f3”
1970-01-01T00:00:00.000000003Z10.0“m1”null
1970-01-01T00:00:00.000000003Z11.0“m1”null
1970-01-01T00:00:00.000000003Z12.0“m1”“f3”
1970-01-01T00:00:00.000000003Z13.0nullnull
null14.0“m1”null
null15.0“m1”null
Output
_timem1_f1m1_f2null_f3null_nullm1_f3m1_null
1970-01-01T00:00:00.000000001Z1.02.03.04.0nullnull
1970-01-01T00:00:00.000000002Z5.06.0null8.07.0null
nullnullnullnullnull9.015.0
1970-01-01T00:00:00.000000003Znullnullnull13.012.011.0