Docs

difference() function

The difference() function computes the difference between subsequent records.
Every user-specified column of numeric type is subtracted while others are kept intact.

Function type: Aggregate
Output data type: Float

difference(nonNegative: false, columns: ["_value"])

Parameters

nonNegative

Indicates if the difference is allowed to be negative. When set to true, if a value is less than the previous value, it is assumed the previous value should have been a zero.

Data type: Boolean

columns

A list of columns on which to compute the difference. Defaults to ["_value"].

Data type: Array of strings

Subtraction rules for numeric types

  • The difference between two non-null values is their algebraic difference; or null, if the result is negative and nonNegative: true;
  • null minus some value is always null;
  • Some value v minus null is v minus the last non-null value seen before v; or null if v is the first non-null value seen.

Examples

from(bucket: "telegraf/autogen")
  |> range(start: -5m)
  |> difference()
from(bucket: "telegraf/autogen")
  |> range(start: -5m)
  |> difference(nonNegative: true)

Example data transformation

Input table
_timeABCtag
0001null12tv
000262nulltv
0003424tv
000410102tv
0005nullnull1tv

With nonNegative set to false

|> difference(nonNegative: false)
Output table
_timeABCtag
0002null1nulltv
0003-202tv
000468-2tv
0005nullnull-1tv

With nonNegative set to true

|> difference(nonNegative: true):
Output table
_timeABCtag
0002null1nulltv
0003null02tv
000468nulltv
0005nullnullnulltv

DIFFERENCE()