Filter complex metric in grafana - influxdb

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.

Related

Import Range Query not importing data

Sorry I know this question has been asked before - I have tried changing my query around but can't seem to get it to work as expected, doesn't look to be anything wrong..
I am simply trying to query data from one large master sheet into a few separate sheets. I am using importrange to get the data from sheet, and a simple select query to filter by one of the columns. If I do a select * I get all the data as expected, but can't use a WHERE clause with any column (I just need 1 of the columns, but I tried with a few different ones).
Appreciate any help!
Query:
=QUERY(IMPORTRANGE("1sNA9u2uQW-XjEKjrVS2a5LtTPCchwSuTkXfjhTJtvPk","Sheet1!B:I"),"select * WHERE 'Rank'='LTC' ")
Columns
Username Rank Time In Service TIS Time In Grade TIG Promotable Awards PLT/SQD
Source sheet: https://docs.google.com/spreadsheets/d/1sNA9u2uQW-XjEKjrVS2a5LtTPCchwSuTkXfjhTJtvPk
Test sheet: https://docs.google.com/spreadsheets/d/1UCucsfE0M4j95d_47iN0LrAhS0luv8wXVMHRVTHJHRQ
As player0 stated, you should refer to the columns by its number, you can select multiple columns and state multiple "where" statements by using Col1,Col2, etc respectively. In this case: Where Col2 = 'LTZ'
try:
=QUERY({IMPORTRANGE("1sNA9u2uQW-XjEKjrVS2a5LtTPCchwSuTkXfjhTJtvPk", "Sheet1!B:I")},
"where Col1='LTC' ", )

Finding duplicates across range and summing it up

I have a list of agents who are working on OT, but the challenge is that they have 2 entries for OT per day, and when i need to work on this data with another sheet i have to sort out the data and remove duplicates manually in order not to cause issues with vlookup.
I need the end result to be like this:
if any agents works OT 2 times a day then sum it up and place in one raw with the date.
i have made helpers columns to show the duplicates but the i got stuck with the next steps
here is the sheet for the example:
https://docs.google.com/spreadsheets/d/1rdqylI9Rr4CxPIJcffEsqfoHx0loPVrqi2UJAQ30Sos/edit#gid=0
use:
=QUERY(A2:C, "select A,B,sum(C) where C is not null group by A,B label sum(C)''")
and vlookup from it...

Is there a way to SUMIFS based on a condition for a output range?

The practical example is: I need to sum the total amount of time worked on a week for specific roles (engineer, etc..) within the company. What I need is to sum a column of values only if the name on the left has a specific role. I could put that role on a column next to the name, but as I would like to keep a history of employee promotion, the role is on a similar week basis table.
Following, we can see the workload tab (with the name and column to sum for example, sum column E) and the people with the name and the roles A21:AB37.
So I am trying to, for example, place the sum of hours(workload) from 'estag 1' people (name matches 'people' tab) on the 'data' tab, on C40
Following is a copy of the Sheets, feel free to update:
https://docs.google.com/spreadsheets/d/1GgQGM7Pca0P3kdZurIBwQP9EMTILswc3sIWF11xXMMM/edit?usp=sharing
I've tried a sumif with filter inside, query, vlookup, but I couldn't get to a working function.
Any suggestions??
try:
=COUNTA(IFERROR(FILTER(people!$A$24:$A, people!C$24:C=$B40)))
and drag down and to the right as needed
UPDATE
=ARRAYFORMULA(SUM(IFERROR(VLOOKUP(IFERROR(
FILTER(people!$A$24:$A, people!C$24:C=$B40), "♥"),
{people!$A$2:$A, people!C$2:C}, 2, 0))))

InfluxDB: Starting cumulative_sum() from zero / aggregate grouping required for cumulative_sum and non_negative_difference

Using InfluxDB, I'm trying produce an output that shows cumulative rainfall for a time period, that starts from zero.
The rainfall sensor outputs a cumulative rainfall amount, but resets to zero on power-failure, restart etc.
My first query component uses non_negative_difference() to show the increments.
select
non_negative_difference(rain) as nnd
FROM
weather
WHERE
$time_query
.... yields an increment per raw data point, for example:
2018-06-01T14:21:00.926Z 0
2018-06-01T14:22:02.959Z 0.30000000000000426
2018-06-01T14:23:04.992Z 0.3999999999999986
2018-06-01T14:24:07.024Z 0.10000000000000142
2018-06-01T14:25:09.059Z 0.19999999999999574
2018-06-01T14:26:11.094Z 0
2018-06-01T14:27:13.127Z 0.10000000000000142
2018-06-01T14:28:15.158Z 0.20000000000000284
2018-06-01T14:29:20.027Z 0.09999999999999432
2018-06-01T14:30:22.476Z 0.10000000000000142
2018-06-01T14:30:53.918Z 0.6000000000000014
2018-06-01T14:31:55.968Z 0.5
2018-06-01T14:32:58.007Z 0.5
2018-06-01T14:34:00.046Z 0.20000000000000284
2018-06-01T14:35:02.075Z 0.3999999999999986
2018-06-01T14:36:04.102Z 0.3999999999999986
2018-06-01T14:37:06.136Z 0.20000000000000284
2018-06-01T14:38:08.201Z 0
So far so good.
I'm now trying to stitch these readings back to cumulative total, starting from zero for the intended period.
I can use cumulative_sum() for this, for example:
SELECT
cumulative_sum(nnd)
FROM
(SELECT
non_negative_difference(rain) as nnd
FROM
weather
WHERE
$time_query )
which yields:
2018-06-01T14:21:00.926Z 0
2018-06-01T14:22:02.959Z 0.30000000000000426
2018-06-01T14:23:04.992Z 0.7000000000000028
2018-06-01T14:24:07.024Z 0.8000000000000043
2018-06-01T14:25:09.059Z 1
2018-06-01T14:26:11.094Z 1
2018-06-01T14:27:13.127Z 1.1000000000000014
2018-06-01T14:28:15.158Z 1.3000000000000043
2018-06-01T14:29:20.027Z 1.3999999999999986
2018-06-01T14:30:22.476Z 1.5
2018-06-01T14:30:53.918Z 2.1000000000000014
2018-06-01T14:31:55.968Z 2.6000000000000014
2018-06-01T14:32:58.007Z 3.1000000000000014
2018-06-01T14:34:00.046Z 3.3000000000000043
2018-06-01T14:35:02.075Z 3.700000000000003
2018-06-01T14:36:04.102Z 4.100000000000001
2018-06-01T14:37:06.136Z 4.300000000000004
2018-06-01T14:38:08.201Z 4.300000000000004
Looking good!
Now I'd like to group it up into more distinct time buckets, for nice graphing.
Let's try....
SELECT
cumulative_sum(max(nnd))
FROM (SELECT
non_negative_difference(rain) as nnd
FROM
weather
WHERE
$time_query)
GROUP BY
time(5m)
and I get an error: ERR: aggregate function required inside the call to non_negative_difference
But I cannot find a reasonable way of adding aggregates and groupings to non_negative_difference() that do not affect the accuracy of the differencing function itself.
The only thing I've been able to do is a dummy aggregate SUM() over time groups that are smaller than the sensor period. But this isn't robust enough for my liking - (and i'm still not sure it is 100% correct)
Is it correct that I must have both queries as aggregate queries?
I was trying to do this very thing for my weather station. Instead of having the weather station calculate the cumulative value I wanted Grafana to do it. The solution that worked for me is the advanced syntax Yuri Lachin mentions in his comments.
With InfluxDB you can use CUMULATIVE_SUM(), but the basic syntax doesn't allow you to group by time (only by tag). The "advanced syntax", however, allows you to to have a time series by nesting an aggregate function like MEAN() or SUM().
Here's the function I am using in Grafana to get a cumulative rainfall total for a selected time period:
SELECT CUMULATIVE_SUM(MEAN("rainfall")) FROM "weather" WHERE $timeFilter GROUP BY time(1h) fill(0).
The GROUP BY is, of course, flexible. I was interested in hourly rainfall so I grouped by 1h. You can group by the time period you find most interesting.
Using this query the rainfall will start from zero for period you select in Grafana. In the Seattle area we had measurable rain (I know, shocker) on 8/6/2020 and 8/8/2020. If I set my date range to include both dates the graph shows just under .2mm total rainfall:
If I switch my graph to 8/8 and 8/9 the total is just under 1mm:
Note: I was also interested in seeing the individual bucket tips so included those as bars on the second Y-axis.
For more detail see: https://docs.influxdata.com/influxdb/v1.8/query_language/functions/#advanced-syntax-7

Using InfluxDB subquery to subtract values

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)

Resources