Hello community.
Is there anyone here who can translate 2 lines from influxQL to Flux for me? Unfortunately I can’t find any comparable examples. It’s about a query of an influxDB 2 database.
SELECT Total_Ticks FROM Weather_station ORDER BY DESC LIMIT 1
…and the second query:
SELECT difference(max(“Total_Ticks”)) FROM “Weather_station” WHERE time>now()-2d and time <= now() group by time(1d) tz(‘Europe/Berlin’)
I already know how to set the filters so that I get the right data:
But then I just don’t know what to do ;-(
For someone who is fit in Flux, this is certainly easy. But for me it is an insurmountable obstacle.
Can someone help please?
from(bucket: “bucket”)
|> range(start: -2h) // Query a time range relative to now
|> filter(fn: (r) => r["_measurement"] == "Weather_station")
|> filter(fn: (r) => r["_field"] == "Total_Ticks")
|> max() // returns the row with the maximum value in a specified column from each input table
|> difference() // returns the difference between subsequent values
|> limit(n: 1) // returns the first n rows
|> yield(name: "your-query")
Since you are getting 1 row with the above query, I am not sure you need to worry about putting in an aggregateWindow(every: 1d, fn: mean) function.
Hello and thank you for the quick reply. I am not sure if I understood everything correctly. The first query in influxQL is: SELECT Total_Ticks FROM Weather_station ORDER BY DESC LIMIT 1.
Translated into Flux:
from(bucket: "bucket")
|> range(start: -2h) // Query a time range relative to now
|> filter(fn: (r) => r["_measurement"] == "weather_station_Igersheim")
|> filter(fn: (r) => r["_field"] == "Total_Ticks")
|> max() // returns the row with the maximum value in a specified column from each input table
Did I understand that correctly?
And for the second query: SELECT difference(max("Total_Ticks")) FROM "Weather_station" WHERE time>now()-2d and time <= now() group by time(1d) tz('Europe/Berlin')
Is the correct “translation”
from(bucket: "bucket")
|> range(start: -2h) // Query a time range relative to now
|> filter(fn: (r) => r["_measurement"] == "Weather_station")
|> filter(fn: (r) => r["_field"] == "Total_Ticks")
|> difference() // returns the difference between subsequent values
|> limit(n: 1) // returns the first n rows
|> yield(name: "your-query")
Is that correct?
What does “|> yield(name: “your-query”)” mean?
And how can I find out if “aggregateWindow(every: 1d, fn: mean)” is necessary?
Sorry, I was going too fast yesterday and did not realize you had 2 queries. The yield statement is needed to put the results into a table that can be viewed in Influx Data Explorer.
I’ll try to post back in the next 24 hours or when time allows.
I am a bit sketchy about the whole timezone thing. I always store data in InfluxDB in UTC and then use the local time zone when I am querying it (in InfluxDB or Grafana).
Try running the above in Influx Data Explorer with Raw Data toggle enabled and see if they produce the correct results.
EDIT: If the above works, try putting this in at the very beginning before the from(bucket) statement, and see if it still works with the correct time:
import "timezone"
// Set location to be Europe/Berlin
option location = timezone.location(name: "Europe/Berlin")
Now it works without error message! Perfect.
Thanks a lot for your support. Without this platform and your help, I probably wouldn’t have been able to do this.