简体   繁体   中英

Calculating MySQL Select field based on previously calculated field

So I have a phone customer database in MySQL, the database has two tables, customer_master and call_data . Both the tables have a customer_id field that matches individual customer numbers with certain attributes. The customer_master table has the customer names, and the call_data has tons of call data associated with a unique customer ID. I need to create a query where I get the number of local calls attempted, and then the number off successful local calls placed (not dropped calls) based on the call events and calculate an average. The call_data table has a call_event field where I essentially need to look for two different things a) local_call_failure and local_call_success . I'm able to create a query where I can use an inner join to display my table with the userID , user name , and total local attempts , however I can't figure out how to then use that calculated attempt value to then calculate the local call success average. Here is how I get the total attempts by picking out the local calls based on call_event :

SELECT customer_id,
CONCAT(name_first, ' ', name_last) AS 'Customer Name',
COUNT(CM.call_event) AS 'Total Local Attemps'
FROM customer_master CM
INNER JOIN call_data CD ON CM.customer_id = CD.customer_id
AND ((call_event = 'local_call_failure')
     OR (call_event = 'local_call_success') )
GROUP BY customer_id;

Now I can create another query to calculate the total successful local calls placed by using the same idea above, except in the AND statement, only include call_event = 'local_call_success' . If I could somehow figure out how to do these independently and then calculate the average by just dividing call_success/tota_call_attempts , that's all I need! In any other language this would take me one loop and five minutes, but SQL seems to be much more difficult. I need to get both calculations completed in the same query, and print them out side by side, then make another column for the average. How can this be accomplished? From what I've been able to gather, setting a variable like @var_name := COUNT(CM.call_event) and then using that variable to perform a calculation again inside your SELECT statement is a no-no in SQL.

Can anyone help me with this? I'd be forever in your debt!

using sql server syntax, which I am sure will be very close to the same:

select customer_id, CONCAT(name_first, ' ', name_last) AS 'Customer Name', 
COUNT(CM.call_event) AS 'Total Local Attemps',
sum(case when call_event = 'local_call_success' then 1 else 0 end) as 'Success Events',
sum(case when call_event = 'local_call_success' then 1 else 0 end)/COUNT(CM.call_event) as SuccesstRate
from customer_master CM
INNER JOIN call_data CD ON CM.customer_id = CD.customer_id
AND ((call_event = 'local_call_failure')
    OR (call_event = 'local_call_success')
    )
group by customer_id;

In MySQL, you can do:

select cm.customer_id,
       concat(cm.name_first, ' ', cm.name_last) AS `Customer Name`, 
       count(*) as total_attempts,
       sum( cd.call_event = 'local_call_failure' ) as num_failures,
       sum( cd.call_event = 'local_call_failure' ) / count(*) as failure_rate
from customer_master cm join
     call_data cd 
     on cm.customer_id = cd.customer_id and
        cd.call_event in ('local_call_failure', 'local_call_success')
group by customer_id, `Customer Name`;

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM