0

I am trying to perform a maths equation in SQL to calculate service up time. I have a column with pass counts and a column with fail counts.

I have achieved this sum in VB for the application side and the sum is as follows: pass / (pass+fail) * 100.

The results should be on some rows 99.98 but I can't get SQL to give me the same result. I either get 100 or 0 as the result.

This is my SQL query:

select pass / sum(pass+fail) * 100 as total, friendlyname from sensors group by pass, friendlyname

Example row:

Friendly Name  | Pass  | Fail
_____________________________

Cloudflare     | 25527 | 23 

So as you can see the result should be 99.90 when done on a calculator using above formula but SQL reports this sum as 0.

Really would be grateful for some help!

Dale K
  • 25,246
  • 15
  • 42
  • 71

2 Answers2

2

Make at least one portion of the division a floating point number, to force that precision. Also, your math seems to be off. If you want to compute the ratio of passes/fails to the total counts, then sums should appear in both the numerator and denominator.

select
    100.0 * sum(pass) / sum(pass+fail) as pass_pct,
    100.0 * sum(fail) / sum(pass+fail) as fail_pct
    friendlyname
from sensors
group by
    friendlyname;
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
0

Try this :

select
    (pass * 100.0) / sum(pass+fail) as total,
    friendlyname
from sensors
group by friendlyname, pass
Amira Bedhiafi
  • 8,088
  • 6
  • 24
  • 60