I have a query I run in my OSS 1.7 influxdb that looks like this:
SELECT mean('temperature') AS MeanTemp, max('temperature') AS MaxTemp, min('temperature') AS MinTemp, mean('flow') AS MeanFlow, max('flow') AS MaxFlow, min('flow') AS MinFlow, sum('credit') AS TotalCredits FROM MyMeasurement WHERE time >= startTime and time <= endTime
The result is a single row that has the mean, min and max values for the temperature and flow and the total credits for the defined time window. I’d like to replicate this in 2.0 cloud via API and came up with the following - it looks kind of messy and I’m sure there must be a better way to do this that is more concise:
credits = from(bucket: "mybucket")
|> range(start: startTime, stop: stopTime)
|> filter(fn: (r) => r.measurement == "MyMeasurement")
|> filter(fn: (r) => r._field == "credit")
temperature = from(bucket: "mybucket")
|> range(start: startTime, stop: stopTime)
|> filter(fn: (r) => r.measurement == "MyMeasurement")
|> filter(fn: (r) => r._field == "credit")
flow = from(bucket: "mybucket")
|> range(start: startTime, stop: stopTime)
|> filter(fn: (r) => r.measurement == "MyMeasurement")
|> filter(fn: (r) => r._field == "credit")
TotalCredits = credits
|> set(key: "_field", value:"TotalCredits")
|> drop(columns: ["_start", "_stop", "_time"])
|> sum()
MaxTemp = temperature
|> set(key:"_field", value:"MaxTemp")
|> drop(columns: ["_start", "_stop", "_time"])
|> max()
MinTemp = temperature
|> set(key:"_field", value:"MinTemp")
|> drop(columns: ["_start", "_stop", "_time"])
|> min()
MeanTemp = temperature
|> set(key:"_field", value:"MeanTemp")
|> drop(columns: ["_start", "_stop", "_time"])
|> mean()
MaxFlow = flow
|> set(key:"_field", value:"MaxFlow")
|> drop(columns: ["_start", "_stop", "_time"])
|> max()
MinFlow = flow
|> set(key:"_field", value:"MinFlow")
|> drop(columns: ["_start", "_stop", "_time"])
|> min()
MeanFlow = flow
|> set(key:"_field", value:"MeanFlow")
|> drop(columns: ["_start", "_stop", "_time"])
|> mean()
union (tables: [MeanTemp, MaxTemp, MinTemp, MeanFlow, MaxFlow, MinFlow, TotalCredits])
|> pivot(rowKey: ["_measurement"], columnKey: ["_field"], valueColumn: "_value")
|> yield()
What’s the best way to perform a query like this?