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
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
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";
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
)