How to sum multiple tags?

I am new to influxdb2, and can’t seem to wrap my head around it yet. So I have a measurement named prices, with a tag named ‘name’, and field named ‘price’. I want to have the sum of all the tags.price for each time slice. How can I do this using the data explorer, or flux?

Thanks.

Hello,

You can use map() and “with” to calculate a new value for each row. There is some documentation here:

you will do something like:

 |> map(fn: (r) => ({r with total: r.tag1 + r.tag2 })

can you do it without having to state the tag specifically? There are a lot of tags, and new ones are added. so something like … total: r.* }} …

@ForceConstant I think the following will work for you:

timeSlice = 1h

from(bucket: "example-bucket")
  |> range(start: -7d)
  |> filter(fn: (r) => r._measurement == "prices" and r._field == "price")
  |> aggregateWindow(every: timeSlice, fn: sum)

@scott
That didn’t seem to work, looks like it just summed the values for each tag for the 1h. I want a single value of the sum of all the tags.

I guess I don’t fully understand the transformation you’re trying to do. Could you provide a sample of the input data schema and an example of the output you’re looking for?

Here is a made data based on my schema, and I want to graph total price of all the items at each time (12h), i.e. 44, 53

prices,item=item1 price=21 1621512000
prices,item=item2 price=23 1621512000
prices,item=item1 price=15 1621425600
prices,item=item2 price=38 1621425600

Ok, I think I understand. Try this:

from(bucket: "example-bucket")
  |> range(start: -7d)
  |> filter(fn: (r) => r._measurement == "prices" and r._field == "price")
  |> group(columns: ["_time", "_measurement"])
  |> sum()

That almost worked, but there is an issue. When I graph, the times are summed, but they are also created as different tables, so the points are not connected. See the pic below of it shown as a table.

Just “ungroup” everything into a single table:

from(bucket: "example-bucket")
  |> range(start: -7d)
  |> filter(fn: (r) => r._measurement == "prices" and r._field == "price")
  |> group(columns: ["_time", "_measurement"])
  |> sum()
  |> group()