Hi,
I wrote this query:
from(bucket: "statisticaldata")
|> range(start: -24h) // we know that data is flushed daily
|> last()
|> filter(fn: (r) => r["_measurement"] == "systemStatus")
|> filter(fn: (r) => r["_field"] == "conveyorSpeed")
The result I get is:
2021-06-07 01:37:26 0
If I change the query to include a sort:
from(bucket: "statisticaldata")
|> range(start: -24h) // we know that data is flushed daily
|> sort(columns: ["_time"], desc: false)
|> last()
|> filter(fn: (r) => r["_measurement"] == "systemStatus")
|> filter(fn: (r) => r["_field"] == "conveyorSpeed")
I get a different reading:
2021-06-07 11:53:05 1502
Why is sort()
necessary to obtain the latest value?
In the documentation it says:
By default, InfluxDB returns results sorted by time, however you can use the
sort()
function to change how results are sorted.first()
andlast()
respect the sort order of input data and return records based on the order they are received in.
Thank you for your help!
P.s.: I’m using influxdb latest container.