Typorithm · hard

group & pivot

flux-group-pivotinfluxdbgrouppivottabular
Saved on this device
Full lesson · 8 learning steps Open any exercise for focused practice
1

Step 1 of 8 · intro card

Learn group & pivot

0%

Flux

group repartitions rows into tables keyed by chosen columns so reducers like mean() run per group. pivot then reshapes long rows into a wide table — one column per field — for tabular output.

Flux carries an implicit grouping; group(columns: ["host"]) regroups rows so each host becomes its own table, and any following reducer runs once per group.

After mean(), each host table holds one average; grouping is how you control the scope a reducer applies to.

pivot(rowKey: ["_time"], columnKey: ["_field"], valueColumn: "_value") turns long-format rows into a wide table: each distinct _field becomes a column, with _value as the cell and _time identifying the row.

01

group repartitions rows into tables by the given columns.

02

Reducers like mean() run per group after grouping.

03

pivot reshapes long rows into a wide, tabular result.

from(bucket: "metrics")
  |> range(start: -1h)
  |> group(columns: ["host"])
  |> mean()
  |> pivot(rowKey: ["_time"], columnKey: ["_field"], valueColumn: "_value")

Read the implementation

Connect each part of the code to the concept before you type.

1. Regroup series

group repartitions rows into tables keyed by the given columns — here one table per host.

group(columns: ["host"])

2. Per-group reduce

mean() now runs per group, producing one average per host table.

mean()

3. Pivot to columns

pivot reshapes long-format rows into a wide table, one column per field value.

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

4. Row key

rowKey lists the columns that identify a row; rows sharing these values merge into one.

rowKey: ["_time"]

A complete learning loop

01

Understand

Read the concept map and follow the code breakdown.

02

Reconstruct

Type the implementation, repair mistakes and work through every recall exercise.

03

Verify

Use the final check without hints; revisit weak concepts in short practice sessions.

group & pivot: Lesson & Practice · Typorithm | ApplyDjinn