Addition of fields found with regex select clause

Hello,

I’m working with influxdb only for some days and now I have a task that sounds simple but I don’t found a solution.

I have one measurement with ~60 fields. Some of the fields store the number of items received from different sensors identified by a name in brackets:

show field keys from ipsb

name: ipsb
fieldKey fieldType



sensor(x1).items float
sensor(x2).items float

sensor(xN).items float

To get the last value for all sensors I can use this command:

SELECT last(/sensor.*.items/) FROM “ipsb”

name: ipsb
time last_sensor(x1).items last_sensor(x2).items … last_sensor(xN).items
0 562 231 … 300

For some calculations I need to sum the last values for all sensors. 562 + 231 + … + 300

I can’t do something like

SELECT last(“sensor(x1).items”) + last(“sensor(x2).items” + last(“sensor(xN).items”) FROM “ipsb”

because I don’t know the exact number of sensors or the correct names.

Is there a way to combine addition of values with a regex select clause?

Hello @kielschwein,
Welcome! What version of InfluxDB are you using? Summing multiple fields at a timestamp is very hard to do with influxQL especially. if you don’t know the names. I recommend enabling flux and using that if you’re running 1.7+.
Here is an example of a query that sums all my fields for cpu0

from(bucket: "my-bucket")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["_measurement"] == "cpu")
  |> filter(fn: (r) => r["cpu"] == "cpu0")
  |> group(columns: ["_time"], mode:"by")
  |> sum()
  |> group()

This topic was automatically closed 60 minutes after the last reply. New replies are no longer allowed.