This topic is locked
[SOLVED]

Help Calculating a percentage from Count

9/25/2021 9:57:36 AM
PHPRunner General questions
A
Andrew S author

Good morning everyone.

PHP Runner V9.8 latest build with mysql database.

I am displaying a totals page which shows the following information which is correct

Selections 39
Won 11
Placed 13
Lost 15
BFP 29

mysql query for the above is:

SELECT
COUNT(*) AS Total Selections,
COUNT(IF(Result='W', 1, NULL)) AS Won,
COUNT(IF(Result='P', 1, NULL)) AS Placed,
COUNT(IF(Result='L', 1, NULL)) AS Lost,
COUNT(IF(Place='Y', 1, NULL)) AS BFPlace
FROM Races
WHERE (Result <> "") AND (RaceSystemID = 32)

I would be grateful if anyone can advise me how to calculate a percentage for each count.

What I would like to achieve is
Selections 39
Won 11 28 percent
Placed 13 33 percent
Lost 15 38 percent
BFP 29 74 percent

Thank you.

Tedy 9/26/2021

try this

SELECT
COUNT(*) AS 'Total Selections',
COUNT(IF(Result='W', 1, NULL)) AS Won,
concat(ROUND(((COUNT(IF(Result='W', 1, NULL)) / COUNT(*)) * 100),2),'%') AS 'Percent',
COUNT(IF(Result='P', 1, NULL)) AS Placed,
concat(ROUND(((COUNT(IF(Result='P', 1, NULL)) / COUNT(*)) * 100),2),'%') AS 'Percent',
COUNT(IF(Result='L', 1, NULL)) AS Lost,
concat(ROUND(((COUNT(IF(Result='L', 1, NULL)) / COUNT(*)) * 100),2),'%') AS 'Percent',
COUNT(IF(Place='Y', 1, NULL)) AS BFPlace,
concat(ROUND(((COUNT(IF(Place='Y', 1, NULL)) / COUNT(*)) * 100),2),'%') AS 'Percent'
FROM Races
WHERE (Result<> "") AND (RaceSystemID = 32)
A
Andrew S author 9/26/2021

Thank you so much Tedy. Been struggling with this for ages. Works perfectly.