reduce() function

reduce() aggregates rows in each input table using a reducer function (fn).

The output for each table is the group key of the table with columns corresponding to each field in the reducer record. If the reducer record contains a column with the same name as a group key column, the group key column’s value is overwritten, and the outgoing group key is changed. However, if two reduced tables write to the same destination group key, the function returns an error.

Dropped columns

reduce() drops any columns that:

  • Are not part of the input table’s group key.
  • Are not explicitly mapped in the identity record or the reducer function (fn).
Function type signature
(<-tables: stream[B], fn: (accumulator: A, r: B) => A, identity: A) => stream[C] where A: Record, B: Record, C: Record
For more information, see Function type signatures.



(Required) Reducer function to apply to each row record (r).

The reducer function accepts two parameters:

  • r: Record representing the current row.
  • accumulator: Record returned from the reducer function’s operation on the previous row.


(Required) Record that defines the reducer record and provides initial values for the reducer operation on the first row.

May be used more than once in asynchronous processing use cases. The data type of values in the identity record determine the data type of output values.


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


Compute the sum of the value column

import "sampledata"
    |> reduce(fn: (r, accumulator) => ({sum: r._value + accumulator.sum}), identity: {sum: 0})

View example input and output

Compute the sum and count in a single reducer

import "sampledata"
    |> reduce(
        fn: (r, accumulator) => ({sum: r._value + accumulator.sum, count: accumulator.count + 1}),
        identity: {sum: 0, count: 0},

View example input and output

Compute the product of all values

import "sampledata"
    |> reduce(fn: (r, accumulator) => ({prod: r._value *}), identity: {prod: 1})

View example input and output

Calculate the average of all values

import "sampledata"
    |> reduce(
        fn: (r, accumulator) =>
                count: accumulator.count + 1,
                total: + r._value,
                avg: float(v: + r._value) / float(v: accumulator.count + 1),
        identity: {count: 0, total: 0, avg: 0.0},

View example input and output

Was this page helpful?

Thank you for your feedback!

Introducing InfluxDB 3.0

The new core of InfluxDB built with Rust and Apache Arrow. Available today in InfluxDB Cloud Dedicated.

Learn more

State of the InfluxDB Cloud Serverless documentation

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.