So I need to divide two scalar values produced by two queries to a single measurement, and I came up with the following solution (I use Grafana for visualisation):
SELECT sum("errors") / sum("total") AS "errorPercentage" FROM (
SELECT count("duration") AS "total" FROM "requests" WHERE [[timeFilter]]
), (
SELECT count("duration") AS "errors" FROM "requests" WHERE [[timeFilter]] AND "result" = 'KO'
)
As you may have guessed, a goal is simple, calculate error percentage for some of my custom requests data.
Each sub-query produces a valid result, but when combined like that they produce nothing.
A bit of debugging information:
If I query like this:
SELECT sum("errors") as "errors", sum("total") as "total" FROM (
SELECT count("duration") AS "total" FROM "requests" WHERE [[timeFilter]]
), (
SELECT count("duration") AS "errors" FROM "requests" WHERE [[timeFilter]] AND "result" = 'KO'
)
A result looks like this:
Switching sub-queries produces a different result:
SELECT sum("errors") as "errors", sum("total") as "total" FROM (
SELECT count("duration") AS "errors" FROM "requests" WHERE [[timeFilter]] AND "result" = 'KO'
), (
SELECT count("duration") AS "total" FROM "requests" WHERE [[timeFilter]]
)
I can’t include more than one image sadly, but result is “errors” field has a valid value, when “total” is empty
Looks like issue is with the second sub-query.
But if I don’t use any aggregate functions on main query like this (sub-queries position is same as for initial variant):
SELECT "errors", "total" FROM (
SELECT count("duration") AS "total" FROM "requests" WHERE [[timeFilter]]
), (
SELECT count("duration") AS "errors" FROM "requests" WHERE [[timeFilter]] AND "result" = 'KO'
)
The result is same as for previous query, where result is “errors” field has a valid value, when “total” is empty
Now second sub-query is calculated, but not the first one.
I’m a bit confused, may be someone will help me with this issue?