InfluxDB 2.0: Compare data of multiple fields and create new field

How to compare 3 fields in the measurement to create new field and set new field as output?
Can you please help me with the query ?

I was trying below query…

from(bucket: "bucketName")

  |> range(start: -3h)
  |> filter(fn: (r) => r["_measurement"] == "measurementName")
  |> filter(fn: (r) => r["_field"] == "col1" or r["_field"] == "col2" or r["_field"] == "col3")
  |> aggregateWindow(every: 5s, fn: last, createEmpty: false)
  |> drop(columns:[list_of_column_names])
  |> map(
        fn: (r) => ({ r with
            _field: "flag",
            _value: if ((r._field == "col1" and r.value > 80) and (r._field == "col2" and r.value > 80) and (r._field == "col3" and r.value > 80)) then 1 else 0,
        })
  )
|> filter(fn: (r) => r["_field"] == "flag")

But it doesn’t seem to give me an expected output…

Hello @Anaisdg,

Could you please help me with the above query.

Thank you in advance.

Hello @dhyaneshnaik,
Thanks for tagging me I’m sorry I missed this. Sometimes questions fall through the cracks. I’d try the following:

import "influxdata/influxdb/schema"

from(bucket: "bucketName")

  |> range(start: -3h)
  |> filter(fn: (r) => r["_measurement"] == "measurementName")
  |> filter(fn: (r) => r["_field"] == "col1" or r["_field"] == "col2" or r["_field"] == "col3")
  |> aggregateWindow(every: 5s, fn: last, createEmpty: false)
  |> drop(columns:[list_of_column_names])
  |> schema.fieldsAsCols()
  |> map(
        fn: (r) => ({ r with
            _field: "flag",
            _value: if r.col1 > 80 and r.col2 > 80 and r.col3  > 80 then 1 else 0
        })
  )
|> filter(fn: (r) => r["_field"] == "flag")

Please let me know if that helps.
Here’s some relevant documentation:

1 Like