I have a table, where questions have been answered either pass or fail.
I just need an SQL query to find the percentage of Fails for question 1 out of the total number of questions asked.
I’m trying this, but this is coming up with a Syntax error –
$CA001 = "COUNT(CA001Result WHERE CA001Result = "Fail") / COUNT(CA001Result)) From Datable";
As I said, total noob, it’s MYSQL btw.
I no need to isolate the % results for Product, I’ve got this so far, and have tried GROUP by Product but that didn’t work, any ideas?
$CA001 = "( SELECT ROUND(100 * (SELECT count(CA001Result ) from Data_Table where (CA001Result='Fail' AND Product='$product' AND Area='$Area'))/count(CA001Result),2) from Data_Table) AS 'CA001 %'";
Looking at it again I need to insert a WHERE Product = ‘Product’ somewhere like where I have it below, however don’t quite understand where to put it as the below doesn’t work:
$CA001 = "( SELECT ROUND(100 * (SELECT count(CA001Result ) from Data_Table where (CA001Result='Fail' AND Product='$product' AND Area='$Area'))/count(CA001Result WHERE Product = '$product'),2) from Data_Table) AS 'CA001 %'";
Suppose this is your table:
So you need to use a subquery like this:
Or you can use
COUNTwithCASElike this:Or you can use
SUMwithCASElike this:Or you can use
SUMwithout usingCASE(Only for MySQL) like this:See this SQLFiddle