Hello @Donoli,
I’m trying to get some values between a start and a stop and i should have the last value before the start and the first value after the stop also… and i’m trying to achieve this with just one query and one return…
Can you just query slightly outside of the start and stop? And then find the first value outside of your start and stop?
Something like:
import "date"
data = from(bucket: "noaa")
|> range(start:(1627653004 - 1000 ), stop: ( 1627655004 + 1000))
|> filter(fn: (r) => r["_measurement"] == "average_temperature")
|> filter(fn: (r) => r["_field"] == "degrees")
|> filter(fn: (r) => r["location"] == "santa_monica")
// |> yield(name: "data exxtended start and stop boundary")
first = data
|> filter(fn: (r) => r._time < time(v:1627653004000000000) )
|> last()
|> findRecord(fn: (key) => true, idx: 0)
// |> yield(name: "first value before start")
last = data
|> filter(fn: (r) => r._time > time(v:1627655004000000000))
|> limit(n: 1)
|> findRecord(fn: (key) => true, idx: 0)
// |> yield(name: "last value after stop")
time_before_start = first._time
time_after_stop = last._time
from(bucket: "noaa")
|> range(start:time_before_start, stop: time_after_stop)
|> filter(fn: (r) => r["_measurement"] == "average_temperature")
|> filter(fn: (r) => r["_field"] == "degrees")
|> filter(fn: (r) => r["location"] == "santa_monica")
Does that do what you expect?
Here’s the annotated CSV:
#group,false,false,true,true,false,false,true,true,true
#datatype,string,long,dateTime:RFC3339,dateTime:RFC3339,dateTime:RFC3339,double,string,string,string
#default,data exxtended start and stop boundary,,,,,,,,
,result,table,_start,_stop,_time,_value,_field,_measurement,location
,,0,2021-07-30T13:33:24Z,2021-07-30T14:40:04Z,2021-07-30T13:35:14.035840903Z,70,degrees,average_temperature,santa_monica
,,0,2021-07-30T13:33:24Z,2021-07-30T14:40:04Z,2021-07-30T13:41:14.035840903Z,81,degrees,average_temperature,santa_monica
,,0,2021-07-30T13:33:24Z,2021-07-30T14:40:04Z,2021-07-30T13:47:14.035840903Z,84,degrees,average_temperature,santa_monica
,,0,2021-07-30T13:33:24Z,2021-07-30T14:40:04Z,2021-07-30T13:53:14.035840903Z,76,degrees,average_temperature,santa_monica
,,0,2021-07-30T13:33:24Z,2021-07-30T14:40:04Z,2021-07-30T13:59:14.035840903Z,85,degrees,average_temperature,santa_monica
,,0,2021-07-30T13:33:24Z,2021-07-30T14:40:04Z,2021-07-30T14:05:14.035840903Z,71,degrees,average_temperature,santa_monica
,,0,2021-07-30T13:33:24Z,2021-07-30T14:40:04Z,2021-07-30T14:11:14.035840903Z,76,degrees,average_temperature,santa_monica
,,0,2021-07-30T13:33:24Z,2021-07-30T14:40:04Z,2021-07-30T14:17:14.035840903Z,89,degrees,average_temperature,santa_monica
,,0,2021-07-30T13:33:24Z,2021-07-30T14:40:04Z,2021-07-30T14:23:14.035840903Z,74,degrees,average_temperature,santa_monica
,,0,2021-07-30T13:33:24Z,2021-07-30T14:40:04Z,2021-07-30T14:29:14.035840903Z,84,degrees,average_temperature,santa_monica
,,0,2021-07-30T13:33:24Z,2021-07-30T14:40:04Z,2021-07-30T14:35:14.035840903Z,71,degrees,average_temperature,santa_monica
So you can use the csv.from() function if you want to test my script on your machine.