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:
ID Answer
-------------
1 , 'pass'
2 , 'pass'
3 , 'fail'
4 , 'fail'
5 , 'fail'
6 , 'fail'
So you need to use a subquery like this:
SELECT 100 *
(SELECT COUNT(answer) FROM t WHERE answer='fail')/COUNT(answer)
AS Fail_percent FROM t;
Or you can use COUNT
with CASE
like this:
SELECT 100 *
COUNT(CASE WHEN answer = 'fail' THEN 1 ELSE NULL END)/COUNT(answer)
AS Fail_percent FROM t;
Or you can use SUM
with CASE
like this:
SELECT 100 *
SUM(CASE WHEN answer = 'fail' THEN 1 ELSE 0 END)/COUNT(answer)
AS Fail_percent FROM t;
Or you can use SUM
without using CASE
(Only for MySQL) like this:
SELECT 100 *
SUM(answer = 'fail')/COUNT(answer)
AS Fail_percent FROM t;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.