Calculating using Variables in MYSQL query

穿精又带淫゛_ 提交于 2019-12-24 18:16:03

问题


hope you can help, this is driving me up the wall

I need to calculate the percentage of times a question has been failed, but this needs to be narrowed down by the geographical area, and product these questions are being asked against.

I have :

$CA002 = "( SELECT ROUND(100 *  (SELECT count(CA002Result) from Data_Table where (CA002Result='Fail'))/count(CA002Result),2) from Data_Table) AS 'CA002 %'";

Which 'works' but just calculates against the whole set of records as an 'overall'

I'm trying :

$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 %'";

And Also :

$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 %'";

and am just getting errors no matter what I try, I just can't seem to work out what I need to put where.

Any great GREATLY apprteciated, thankyou.


回答1:


Try this

//Filter by Area

create table t( id int,  answer varchar(10),Area varchar(10));
insert into t select 1  ,  'pass' , 'Area1';
insert into t select 2  ,  'pass' , 'Area1';
insert into t select 3  ,  'fail' , 'Area1';
insert into t select 4  ,  'fail' , 'Area1';
insert into t select 5  ,  'fail' , 'Area1';
insert into t select 6 ,   'fail' , 'Area2';

SELECT 
        (x.TotalFailedAnswerRecord * 100) /y.TotalRecord AS Fail_percent
FROM
            (   SELECT Area,TotalFailedAnswerRecord = COUNT(answer) 
                FROM t 
                WHERE answer='fail' AND Area = 'Area1' 
                GROUP BY Area
            )x
INNER JOIN
            (   SELECT Area,TotalRecord = COUNT(answer) 
                FROM t 
                WHERE Area = 'Area1'
                GROUP BY Area
            )y ON x.Area =y.Area

//Result
Fail_percent
-------------
60

//Filter by Area,Product

create table t( id int,  answer varchar(10),Area varchar(10),Product varchar(10));
insert into t select 1  ,  'pass' , 'Area1' ,'Product1';
insert into t select 2  ,  'fail' , 'Area1' ,'Product1';
insert into t select 3  ,  'fail' , 'Area1' ,'Product1';
insert into t select 4  ,  'fail' , 'Area1' ,'Product1';
insert into t select 5  ,  'fail' , 'Area1' ,'Product2';
insert into t select 6 ,   'fail' , 'Area2' ,'Product2';

SELECT 
        (x.TotalFailedAnswerRecord * 100) /y.TotalRecord AS Fail_percent
FROM
            (   SELECT Area,Product,TotalFailedAnswerRecord = COUNT(answer) 
                FROM t 
                WHERE answer='fail' AND Area = 'Area1' AND Product = 'Product1' 
                GROUP BY Area,Product
            )x
INNER JOIN
            (   SELECT Area,Product,TotalRecord = COUNT(answer) 
                FROM t 
                WHERE Area = 'Area1' AND Product = 'Product1'
                GROUP BY Area,Product
            )y ON x.Area =y.Area AND x.Product = y.Product

//Result
Fail_percent
-------------
75

Hope this helps



来源:https://stackoverflow.com/questions/12164092/calculating-using-variables-in-mysql-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!