Sebastian
Sebastian

Reputation: 5999

InfluxDB get maximum value of moving average

To get the moving average of a series in InfluxDB you can do the following:

SELECT MOVING_AVERAGE(<field_key>,<window>) FROM <measurement_name>

(from the influxdb documentation)

That will return a dataset with the moving average on each point. However I would like to get only the maximum of that dataset.

Of course I could do that in my script but then I would have to download all the data of the moving average first, and then process it locally. (My InfluxDB server is not on the local network)

Is there a way to get only the maximum of the moving average without getting the complete moving average data?

Upvotes: 1

Views: 3342

Answers (2)

Sebastian
Sebastian

Reputation: 5999

Thanks to Nate Good who pointed me towards subqueries I ended up with the following solution:

SELECT MAX(moving_average) FROM (SELECT MOVING_AVERAGE(<field_key>,<window>) FROM <measurement_name>)

That will give you a max value and the time it occured. For my own case (using the python client) it gave me this result:

ResultSet({'('power', None)': [{'max': 17147.76, 'time': '2016-08-19T19:18:48Z'}]})

Upvotes: 1

Nate Good
Nate Good

Reputation: 439

Version 1.2 was recently released with support for subqueries https://docs.influxdata.com/influxdb/v1.2/query_language/data_exploration/#subqueries

That would be one way to solve your problem. You could probably also do a GROUP BY using a large time interval. And as I look at the docs they even show that:

The MOVING_AVERAGE() query with a nested function and a GROUP BY time() clause:

SELECT MOVING_AVERAGE(<function>(<field_key>),<window>) FROM <measurement_name> WHERE <stuff> GROUP BY time(<time_interval>)

Upvotes: 2

Related Questions