How to sum the value across tags using flux?

Hi there, i’m new to flux. I’ve got a very simple question. Suppose my data is like this:

measurement,tag=tag1 value=1 2019-07-01
measurement,tag=tag2 value=2 2019-07-01
measurement,tag=tag3 value=3 2019-07-01
measurement,tag=tag1 value=4 2019-07-01
measurement,tag=tag2 value=5 2019-07-01
measurement,tag=tag3 value=6 2019-07-01

i’d like to get the total amount of value in one day, which is:

value=21 2019-07-01

but when i use the query:

from(bucket: "bucket")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> aggregateWindow(every: 24h, fn: sum)

the db returns me like this:

tag=tag1 value=5 2019-07-01
tag=tag2 value=7 2019-07-01
tag=tag3 value=9 2019-07-01

So my question is how to sum the value across tags?

i’m using version 2.0.0-alpha

hi @kilik52, influxdb is grouped by measurement+tags by default. What this means is that when we aggregate, it will perform aggregation within each separate group and keep the results separate. So even when you create windows using AggregateWindow() within each window, there is a group for each tag value which is why you are seeing the output that you provided.

for your case, you’ll need to ungroup your data. This data is very simple, so |> group() |> aggregateWindow(...) will do what you want, placing all data into one single group before windowing.

in more complex cases, you may need to filter by _field or keep some grouping to prevent errors, e.g., group(columns: ["_measurement", "_field"]) |> aggregateWindow(...)

2 Likes

Thank you very much!