I have a Influx database that is getting filled with values. These values are presented by Grafana. What I need is to get the actual values depending on the selected time interval.
Currently I have the following query for a single metric:
SELECT mean("value") FROM "table" WHERE $timeFilter GROUP BY time($interval) fill(null)
What I want is to subtract the lowest value from that interval, so it only counts the values from within that interval. So the graph needs to start at zero. To get the lowest value from that interval I use:
SELECT min("value") FROM "table" WHERE $timeFilter
So I thought combining those two (with a subquery) like this should work:
SELECT mean("value") - (SELECT min("value") FROM "table" WHERE $timeFilter) FROM "table" WHERE $timeFilter GROUP BY time($interval) fill(null)
Unfortunately this doesnt work. The query is not accepted as a subquery.
This is possible using InfluxDB's built-in functions:
SELECT cumulative_sum(difference((mean("value")))) FROM "table" WHERE $timeFilter GROUP BY time($interval) fill(null)
This takes the difference between consecutive data points (ignoring the absolute value), and cumulatively sums this over the selected time range.
I'm not entirely sure, but this query requires an additional mean() associated with the GROUP BY clause. Perhaps it's better to use max or first instead, depending on your needs.
Update
Alternatively, you can indeed use subqueries and GROUP BY to achieve this, but this way you can only get time ranges that can be represented by GROUP BY (e.g. from 14:13 to 15:16 on July 6 2010 is more difficult).
SELECT (energy_live-energy_fill) as Energy FROM
(SELECT first(value) as energy_fill from energyv3 WHERE $timeFilter GROUP BY time(1d)),
(SELECT first(value) as energy_live from energyv3 WHERE $timeFilter GROUP BY time($__interval))
fill(previous)
Related
I am trying to get the MAX/MIN values of an interval of time, and I would like to get the correspond timestamp of the value.
If I run: SELECT max(value) FROM data WHERE time > 1549034249000000000 and time < 1550157449000000000 GROUP BY time(10s)
I am receiving the timestamp of the range beginning instead of the max(value) timestamp.
What alternatives could there be for receiving the max(value) of an interval and his timestamp?
In SQL is possible to execute a query like: SELECT value DENSE_RANK () OVER (PARTITION BY time ORDER BY variableName DESC) AS Rank FROM tableName Is not possible to run something like that in InfluxDB?
Yout can not. When you use group by you get ever the beginning timestamp of the group by.
The alternative is not to use group by.
for example, temperature data from two different locations stored in one measurement, like
time temperature location
---- ---- ----
1 15 A
2 20 B
3 17 A
4 18 B
How to calculate the difference between temperatures at A and B with querying?
i.e. expecting something like “SELECT a.temperature - b.temperature FROM measurement WHERE location = ‘A’ as a WHERE location = ‘B’ as b”
Thanks
You can use subqueries for that e.g.
SELECT last("temp_a") - last("temp_b") FROM (
SELECT "gauge" AS "temp_a" FROM "measurement" WHERE ("location"='A') AND $timeFilter
),(
SELECT "gauge" AS "temp_b" FROM "measurement" WHERE ("location"='B') AND $timeFilter
) GROUP BY time($__interval) fill(null)
Influx query language does not support functions across measurements.
I have a sheet holding raw data that I would like to filter and sort in another sheet.
Raw data sheet
The goal of the sheet is:
Show only the highest Rank per Name at Level 12 or below
Sort the results from highest to lowest Damage
I show and sort all items with a Level of 12 or below using ORDER BY:
=ARRAYFORMULA(QUERY(RAW!2:1000, "select A,B,D where C<=12 and A<>'' order by D desc"))
Sorted by Damage
But this still shows too many entries. I can filter the entries using MAX() and GROUP BY:
=ARRAYFORMULA(QUERY(RAW!2:1000, "select A,max(B) where C<=12 and A<>'' group by A label max(B) ''"))
Filtered to only show highest rank
However, I cannot sort by DAMAGE or else I get this error:
Unable to parse query string for Function QUERY parameter 2:
COL_IN_ORDER_MUST_BE_IN_SELECT: `D`
And attempting to add that column to the SELECT clause gives this error:
Unable to parse query string for Function QUERY parameter 2:
ADD_COL_TO_GROUP_BY_OR_AGG: D
The error means that I cannot include fields that are not either aggregate formulas (like MAX) or are not a part of the GROUP BY (which DAMAGE is not).
My goal is this:
Filtered to only show highest rank and sorted by damage
How would I go about querying the first sheet to end up with the above sheet?
Here is the actual sheet. Please feel free to fork if you'd like to test an solution.
https://docs.google.com/spreadsheets/d/1XG3eTSc-8eYRh-6ekq_2BjZgleUVCz3v45PLozXe8Xo/edit
If you work from your query (arrayformula is not needed)
=QUERY(Raw!2:500, "select A,B,D where C<=12 and A<>'' order by D desc")
You can filter the query results to get your desired output:
=FILTER(A2:C,MMULT((A2:A=TRANSPOSE(A2:A))*(B2:B<TRANSPOSE(B2:B)),SIGN(ROW(B2:B)))=0)
I have not had success combining the two.
This is possibly a trivial answer, but might help to clarify your requirements. In the case of your test data where max(damage) is in the same row as max(rank), you can just put
=ARRAYFORMULA(QUERY(Raw!2:1000, "select A,max(B),max(D) where C<=12 and A<>'' group by A order by max(D) desc label max(B) '', max(D) ''"))
If what you want is to sort in general by 'the value of damage that corresponds to the highest rank' then the answer is more complicated!
Is it possible to filter values from comlex metric in grafana?
For example:
SELECT sum(one) + sum(two) FROM "table" WHERE $timeFilter GROUP BY time($interval)
I need to show only positive sum sum(one) + sum(two) > 0
In sql I would use alias and HAVING clause like:
SELECT sum(one) + sum(two) AS S FROM "table" WHERE $timeFilter GROUP BY time($interval) HAVING S > 0
However that does not work in grafana.
How can I achieve this result without creating a new sum column in back-end database?
[EDIT]: My grafana GUI looks like this:
After clicking on "pen" button:
As of August 2016, the HAVING clause is not yet available in InfluxDB so finding all points where sum(one) + sum(two) > 0 is not possible directly in InfluxDB without using a continuous query to create an intermediate series.
However, Grafana does allow a minimum y-axis value to be set, which means any negative values will not be shown.
Hope that helps!
This answers just a part of your question but I was able to do the following in grafana+influxdb datasource:
Which selects the sum of one value if >0.
The problem is that it is not possible to select two different values in one query. But maybe you can workaround this problem with two querys in one graph.
I am trying to select some values from influxdb datbase. For this below query, the output is null but the values are there.
SELECT mean(load1), max(load1) FROM system WHERE time >= '2016-02-22T09:47:00Z' and time <= '2016-02-22T09:47:00Z' AND ( host ='daniel-Vostro-2520' ) GROUP BY time(1m);
I need to take values between 1 minute from particular time.
What is wrong in the query.
You have the same time (2016-02-22T09:47:00Z) defined twice in two different clauses. Nothing will match because you basically defined a time interval of 0.