what is the most efficient way, having to measurements (or even more), two output a third measurement which contains only the maximum between the previous two at every time instant?
@vitttosa You have to pivot the values you want to compare into columns so they exist side by side in a row. For example, if you have two fields, fieldA
and fieldB
, and you want only use the greater of these two values, you’d do the following:
- Query both fields.
- Pivot the fields into columns in each row.
- Use
map()
to iterate over each row andmath.mMax()
to return the greater of two fields.
import "math"
from(bucket: "example-bucket")
|> range(start: -1h)
|> filter(fn: (r) => r._measurement == "example-measurement")
|> filter(fn: (r) => r._field == "fieldA" or r._field == "fieldB")
|> pivot(rowKey: ["_time"], columnKey: ["_field"], valueColumn: "_value")
|> map(fn: (r) => ({r with _value: math.mMax(x: r.fieldA, y: r.fieldB)}))
|> drop(columns: ["fieldA", "fieldB"])
1 Like