Search code examples
sqlinfluxdbcontinuous

Continuous query to calculate difference in InfluxDB


Most common use for continuous query is to calculate average etc.

CREATE CONTINUOUS QUERY minnie ON world BEGIN SELECT min(mouse) INTO min_mouse FROM zoo GROUP BY time(30m) END

I have data points with increment values I am attempting to calculate the difference in value in every interval.

INSERT mydb value=4  1470101034546846145
INSERT mydb value=8  1470101042290558132
INSERT mydb value=10 1470101043594271416 

I want to get 10 - 4 = 6 for example.

Is there a way to achieve this using continuous query?


Solution

  • Try using the new DIFFERENCE() function in your query: https://docs.influxdata.com/influxdb/v0.13/query_language/functions/#difference

    SELECT DIFFERENCE(<function>(<field_key>)) FROM <measurement_name> WHERE <stuff> GROUP BY time(<time_interval>)

    This is available since v0.13 I think.