InfluxDB - Getting only last value in query

dukebody picture dukebody · Mar 22, 2015 · Viewed 74.8k times · Source

Is possible to query only for the last value (or n-th value) of the results of a query?

For example, in the query:

SELECT value FROM response_times WHERE time > now() - 1h limit 1000;

Is possible to get only the last value, i.e. the one more far ago in time (possibly the 1000-th element)?

Of course I can retrieve them all and then skip to the last one, but I don't want to waste bandwith this way.

Answer

archos picture archos · Mar 23, 2016

If you are using InfluxDB 0.8 dont use FIRST() or LAST() if you have not GROUP BY because its very slow :(

So if you want to get the these Values you shoud use:

First Value:

SELECT * FROM <SERIES> GROUP BY * ORDER BY ASC LIMIT 1

Last Value:

SELECT * FROM <SERIES> GROUP BY * ORDER BY DESC LIMIT 1

Don't delete the GROUP BY * because then it could be possible that you get unexpected values then.