Percentage calculation using sub-query returns the wrong result

Hi all faced with division issue while percentage calculation using subqueries.

My goal is to execute the formula percentage = (f1000_value * 100)/total_value; The f1000_value represents the count of the requests where response time is less or equal 1000 ms.

So I built the query like this:

SELECT  (mean("f1000")*100) /mean("total") as percentage 
FROM
(SELECT count("responseTime") as "f1000" FROM "requestsRaw" WHERE  "requestName" =~ /->/ AND "requestName" !~ /^SCRIPT/ AND $timeFilter AND responseTime <=1000 ),
(SELECT count("responseTime") as "total" FROM "requestsRaw" WHERE  "requestName" =~ /->/ AND "requestName" !~ /^SCRIPT/ AND $timeFilter)
GROUP BY time([[aggregation]]s), percentage 

But Iā€™m getting wrong value, percentage = (1840*100)/2792; percentage = 65.9; Each subquery returns right result. But the calculated value was unexpected.

I tried to set the exact value of total and I got the right percentage value. Please see screen bellow

Could you please help to understand the issue?

Hello @Michael_Derevyanko,
Can you please provide the output of your two sub queries?
This might be a silly question, but did you mean to include mean("f1000") in your query? It seems like your subqueries/count() should just return one value each.

Hi @Anaisdg

It seems the sub quires returns right values

Hello. @Michael_Derevyanko,
what happens when you remove the mean() from your percentage query?

Hi @Anaisdg

It seems that my query is not correct, due to ā€œNo data pointsā€ message

Could you please give me an example?

@Anaisdg, I would like to add some things ā€¦

No issues for the query like that:
SELECT sum(ā€œcountErrorā€)/sum(ā€œcountā€)*100 AS ā€œrateā€ FROM ā€œ$measurementā€ WHERE ā€œtransactionā€=ā€˜allā€™ AND $timeFilter AND ā€œapplicationā€ =~ /$scenario/

But the issue with subquery is actual for me, my project has blockedā€¦ I spent several days to solve the issue, have no luckā€¦ I tried another dbā€¦ the same issueā€¦ so the point is to get two different numbers from two subqueries then play with that

After investigation, Iā€™ve found out that subtraction works perfectly.

Looks like an issue for the division operations from my point of view while using subqueries like my. Please corrector me if Iā€™m wrong.

That is odd. I donā€™t know. This query works for me.
SELECT "count_n_containers_running"/"count_n_containers_stopped"*100 FROM (SELECT count("n_containers_running") AS "count_n_containers_running", count("n_containers_stopped") AS "count_n_containers_stopped" FROM "docker"."autogen"."docker" WHERE time > :dashboardTime: GROUP BY time(:interval:) FILL(null))
I havenā€™t had problems with the division. Have you tried the query on influxdb directly? In the meantime Iā€™m asking around to see I can find some answers. It seems like itā€™s a grafana parsing issue.

Hi @Anaisdg

I have found the workaround, I added variable - the query to calculate the total request and then applied the variable in the target query (where the percentage is calculating) instead of sub-query using. I hope my investigation can help to someone. So the issue has been solved for my project.
Anyway thank you for the support!

1 Like

This topic was automatically closed 60 minutes after the last reply. New replies are no longer allowed.