希望你能提供帮助,这正在推动我上去
我需要计算一个问题失败的次数百分比,但这需要根据地理区域进行缩小,并且产品会针对这些问题提出质疑。
我有:
$CA002 = "( SELECT ROUND(100 * (SELECT count(CA002Result) from Data_Table where (CA002Result='Fail'))/count(CA002Result),2) from Data_Table) AS 'CA002 %'";
哪个“有效”,但只是将整个记录集计算为“整体”
我正在尝试:
$CA001 = "( SELECT ROUND(100 * (SELECT count(CA001Result) from Data_Table where (CA001Result='Fail' AND Area ='$Area'))/count(CA001Result) from Data_Table WHERE (Area='$Area'),2) AS 'CA001 %'";
另外:
$CA001 = "( SELECT ROUND(100 * (SELECT count(CA001Result ) from Data_Table where (CA001Result='Fail' AND Product='$product' AND Area='$Area'))
/ count(CA001Result WHERE Product = '$product' AND Area='$Area'),2) from Data_Table) AS 'CA001 %'";
同样尝试这一点,但得到MYSQL语法错误。
$CA001 = "( SELECT ROUND(100 * (SELECT count(CA001Result) from Data_Table where (CA001Result='Fail'))/ (SELECT (count(CA001Result) from Data_Table WHERE (Area='$Area')),2) AS 'CA001 %')";
无论我尝试什么,我只是得到错误,我似乎无法解决我需要放在哪里。
尝试以下答案:
$CA001 = "(SELECT 100 * SUM(IF(CA001Result='Fail', 1, 0)) / COUNT(CA001Result) as 'CA001 %' WHERE Area='$Area')";
$CA002 = "( SELECT ROUND(100 * (SELECT count(CA002Result) from Data_Table where (CA002Result='Fail'))/count(CA002Result),2) from Data_Table) AS 'CA002 %'";
但是我将以下内容视为错误:
MySQL错误:您的SQL语法出错;查看与您的MySQL服务器版本对应的手册,以便在'附近使用正确的语法'(SELECT ROUND(100 *(来自Data_Table的SELECT count(CA002Result))(CA002Re'在第1行
任何帮助,非常感谢,谢谢。
编辑 - 使用以下方法破解:
$CA001 ="ROUND ((SELECT 100 * SUM(IF(CA001Result='Fail', 1, 0)) / COUNT(CA001Result)
FROM Data_Table
WHERE Area='$Area'),2) as 'CA001 %'";
非常感谢你的帮助!
答案 0 :(得分:2)
试试这个
SELECT 100 * SUM(IF(CA001Result='Fail', 1, 0)) / COUNT(CA001Result) as 'CA002 %'
FROM Data_Table
WHERE '$product' AND Area='$Area'
答案 1 :(得分:0)
我只是猜测你的模型,但你应该尝试这种模式:
SELECT
Area,
Product,
COUNT(*) as total,
SUM(CA002Result='Fail') as total_fail,
SUM(CA002Result='Fail') / COUNT(*) as 'CA002 %'
FROM Data_Table
GROUP BY Area, Product;