1

I'm trying to select a field from a measurement depending on another fields value (with Flux)

A SQL-Statement would look like this:

SELECT column1 FROM table WHERE column2 > 100;

Thx in advance for any help

amigian74
  • 77
  • 1
  • 9
  • Did you ever find an answer? Amerousful's answer doesn't seem to work - it filters out the field you're interested in. – MW. Oct 19 '22 at 11:35

2 Answers2

0

I'm not sure if this kills performance (specifically the fieldsAsCols call), but it works. It filters by a tag, then pivots the table so that all fields become their own columns, then it filters by field1. Finally it takes the last row (if field2 exists on it).

import "influxdata/influxdb/schema"

from(bucket: "my-bucket-name")
  |> range(start: 2015-07-17T20:32:58.662703915Z, stop: 2022-12-17T20:32:58.662703915Z)
  |> filter(fn: (r) => r["_measurement"] == "my-measurement-name")
  |> filter(fn: (r) => r.myTagName == "2")
  |> schema.fieldsAsCols()
  |> filter(fn: (r) => r.field1 == "some_random_field_value")
  |> last(column: "field2")
  |> yield()

This would be equivalent to:

SELECT field2 WHERE field1 = "some_random_field_value";
MW.
  • 12,550
  • 9
  • 36
  • 65
-1

Please check official documentation. There is many example and explanation for Flux:

  from(bucket: "bucket")
      |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
      |> filter(fn: (r) => r["_measurement"] == "table")
      |> filter(fn: (r) => 
              r["_field"] == "column1" and 
              r["column2"] > 100
            )
Amerousful
  • 2,292
  • 1
  • 12
  • 26