Aggregating by Months or Years in InfluxDB with Flux
By Jonathan Sternberg / Dec 02, 2019 / Community, Developer, Flux
Over a year ago, we made our first commit and commitment to supporting a much wanted and needed feature for Flux. It was a feature that we have wanted as far back as the 1.x versions of InfluxDB, but it was one we were never able to implement as part of the database. Until now.
Recently, we merged and deployed a change to the Flux language to support months and years as special intervals when windowing. Today, I am going to show you how you can use this feature and the power it can help bring to analyzing your data.
This demo uses the NOAA sample dataset located here. I modified the input slightly to remove the lines related to 1.x and used curl to write the data to a new bucket. I then wrote the following code in Flux.
from(bucket: "NOAA") |> range(start: 2015-08-01T00:00:00Z, stop: 2015-10-01T00:00:00Z) |> filter(fn: (r) => r._measurement == "h2o_feet" and r._field == "water_level") |> aggregateWindow(every: 1mo, fn: mean)
And it worked! When I ran this query, I get two points per series. One of them gives me the mean for the month of August and one gives me the mean for the month of September. But this doesn’t only apply to 1 month. I can also use
1y to aggregate across years and it will account for leap years. If I wanted to aggregate how many requests happened each quarter, I would just use
We hope that this feature will help people to better understand their data and we hope to continue expanding the functionality of Flux to help enable a better understanding of data.