Difference between max sum and min sum

Hi

I have values with a «total» field.
I would like to have the difference between MAX and MIN values

At the moment, I have this query:

data = from(bucket: "shelly")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["_measurement"] == "plug")
  |> filter(fn: (r) => r["_field"] == "total")
  |> filter(fn: (r) => r["id"] == "3CE90EC872A8")

min = data
  |> min()

max = data
  |> max()

join(tables: {min: min, max: max}, on: ["_start", "_field", "_measurement"], method: "inner")
  |> map(fn: (r) => ({ r with _value: r._value_max - r._value_min }))

That’s what I want… except the " |> filter(fn: (r) => r[“id”] == “3CE90EC872A8”)" part.

I would like to remove the filter, select all IDs, sum their «total» field, then make the same MAX-MIN.

The idea is to have the total for the interval selected.

Do you have an idea on how to do this ?

Hello @chindit,
Thanks for your question!
You would do:

data = from(bucket: "shelly")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["_measurement"] == "plug")
  |> filter(fn: (r) => r["_field"] == "total")
  |> group()

This way you can get the min, max, and mean vales across all tags.

@chindit This is actually what spread() is designed to do:

data = from(bucket: "shelly")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["_measurement"] == "plug")
  |> filter(fn: (r) => r["_field"] == "total")
  |> filter(fn: (r) => r["id"] == "3CE90EC872A8")
  |> spread()