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
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.
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.
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.
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!