hfi
May 20, 2025, 8:48pm
1
Hi.
I’m new to influxdb and have to say the official docs on query functions are so abstract, that they are no help at all…
I guess my problem is fairly easy but I can’t seem to find a solution.
I have a measurement “temperature”, in there I have 2 fields “sensor1” and “sensor2”.
I want to find the absolute minimum accross both fields (sensors).
What I have is:
from(bucket:"mybucket")
|> range(start: v.timeRangeStart, stop: v.timeRangeStop)
|> filter(fn: (r) => r._measurement == "temperature")
|> min()
this will give me the min for each field/column/sensor … but how can I merge them together?
I mean to get (in pseudo code) if min(sensor1) < min(sensor2) { return min(sensor1) } else { return min(sensor2) }
Hope it becomes clear what I want to achieve.
hfi
May 23, 2025, 5:53pm
2
For everyone how finds this in the future, AI was in the end able to help me (with some slight modifications).
This is what you are looking for:
s1_data = from(bucket: "mybucket")
|> range(start: v.timeRangeStart, stop: v.timeRangeStop)
|> filter(fn: (r) => r._measurement == "temperature" and r._field == "sensor1")
s2_data = from(bucket: "mybucket")
|> range(start: v.timeRangeStart, stop: v.timeRangeStop)
|> filter(fn: (r) => r._measurement == "temperature" and r._field == "sensor2")
union(tables: [s1_data, s2_data])
|> group(columns: [])
|> min()
The trick is you have to store single field queries in variables before joining them.
You can then go ahead and replace min() with mean() to get the average or max() for the maximum.
1 Like