Strange results from InfluxDB functions

I’m using Grafana with InfluxDB 1.1.

Here is the data that I have selected in my current view (also comma separated at the bottom in case you want to copy it):

I get strange results from some InfluxDB queries (with “tag_id” as a templating variable).

SELECT median("field1") FROM "measurement1" WHERE "tag_id" =~ /^$id$/ AND $timeFilter GROUP BY time($interval) fill(null)

A singlestat panel with this query shows 45.6 which is obviously not the median of field1. mean("field1") also gives the same wrong result (45.6).

However, the same query with sum("field1") yields the correct result (45.6). count("field1") is also correct (8).

Then I’d like to divide the sum of field1 by the sum of field2:

SELECT sum("field1")/sum("field2") FROM "measurement1" WHERE "tag_id" =~ /^$id$/ AND $timeFilter GROUP BY time($interval) fill(null)

This shows 6.53 in the singlestat panel. While the correct result is 0.8144 (= 45.6/55.9911).

Why is the result of median(), mean(), and sum()/sum() wrong here?

Table (comma separated):

timestamp,field1,field2
20.02.16 13:03,11.1,7.053333
12.04.16 07:04 4,7.178889
16.04.16 13:47 ,8.7,6.644167
21.04.16 15:58 ,7.75,7.283333
27.04.16 08:41 ,3.45,6.976667
02.05.16 07:55,1.9,7
11.05.16 09:52 ,3.9,7.062778
17.05.16 14:05 ,4.8,6.791944

Just to be sure, what value have you chosen for the Stat field (Options tab for Singlestat in the Value section)? Is it current?

Can you check the raw query and response under the network tab in the Chrome Developer Tools? Here’s a guide.

Great hint and I now have the solution. It was the GROUP BY time($interval). I removed it and now it shows the correct values, independent of the value I choose for the stat field.

Raw queries are:
SELECT sum("field1")/sum("field2") FROM "measurement1" WHERE "tag_id" =~ /^2339$/ AND time > 1455783857s and time < 1463780679s fill(null)
and
SELECT median("field1") FROM "measurement1" WHERE "tag_id" =~ /^2339$/ AND time > 1455783857s and time < 1463780679s fill(null)

Very dangerous to not have a group by time, if you increase time range InfluxDB could return millions of data points and hang your browser

Gotcha! But do you see why the query with the group by returns a wrong result?
I honestly don’t. But would be great to have a solution with group by that returns the correct result.

Ask InfluxDB support, not sure why it would be different

1 Like