Geoff_S
Geoff_S

Reputation: 5105

Syntax for a custom formula in mysql

I have an existing mysql query that works appropriately but I need to add three averages/percentage formulas to it. This query is for call center metrics.

My real issue is in my third metric, missed_call_score. I need to do something like this:

(missed calls / total talk time) * (average calls per CSR / total calls ) * 100. Now, I have missed calls, total talk time and total calls in the query below, but in order to get my number for average calls per csr I need to take out the highest and lowest total calls (out of 15 CSRs) so I'm left with 13 totals. I need to add those totals and I assume divide by 13, which would give me the average per CSR after removing highest/lowest.

My question is how would I incorporate that into my query?

Here's the necessary part of my existing query:

SELECT 
 , extension
 , Total_Outbound+Total_Missed+Total_Received AS Total_Calls
 , Total_Missed
 , Total_Talk_Time_minutes

 /*Here I'll add average_TT_day, average_TT_call, missed_call_score*/

FROM (
  SELECT 
  , c.extension  
  , sum(if(Answered = 1,0,1)) AS Total_Missed     
  , round(sum(Duration) / 60,2) AS Total_Talk_Time_minutes

  /*Here I'll have my new averages and score formula */

    FROM ambition.session a
    INNER JOIN ambition.callsummary b ON a.NOTABLECALLID = b.NOTABLECALLID
  WHERE b.ts between curdate() - interval 5 day and now()

  GROUP BY c.extension  
 ) ;

So I need to add my three metrics to my select statements where I've left the comment. Most importantly, for the missed call score, it needs to look something like this:

(Total_Missed/Total_Talk_Time_Minutes) * ((SUM(all total calls - highest and lowest) / 13) / sum of total_calls) * 100

Obviously that's pseudo code and not totally correct, but an idea.

Last, here's a sample of what I should get:

extension | Total calls | missed calls | total talk time | missed call score
----------------------------------------------------------------------------
1               10              5               20              6.5     
2               8               2               15              3.4             
3               5               3               10              7.8
4               2               2               5               10.4

Formulas:

Total call sum = 25
Total call sum without high and low = 13
average calls per CSR = (13/2) = 6.5

extension 1 = (5/20) * (6.5/25) * 100
extension 2 = (2/15) * (6.5/25) * 100
extension 3 = (3/10) * (6.5/25) * 100
extension 4 = (2/5) * (6.5/25) * 100

Upvotes: 0

Views: 221

Answers (1)

Cloud
Cloud

Reputation: 237

Without access to your data, this is hard. But something like this should point you in the right direction:

SELECT TOP 95 PERCENT
 , extension
 , Total_Outbound+Total_Missed+Total_Received AS Total_Calls
 , Total_Missed
 , Total_Talk_Time_minutes
 , banana.Total_Talk_Time_minutes / SUM(banana.[row]) AS average_TT_call
 , (Total_Missed / Total_Talk_Time_minutes) * ([CallsPerExt].county / SUM([banana.row])) AS [Missed Call Score]

 /*Here I'll add average_TT_day, average_TT_call, missed_call_score*/

FROM (
  SELECT TOP 95 PERCENT
  , c.extension  
  , sum(if(Answered = 1,0,1)) AS Total_Missed     
  , round(sum(Duration) / 60,2) AS Total_Talk_Time_minutes
  , 1 AS [row]

  /*Here I'll have my new averages and score formula */

    FROM ambition.session a
    INNER JOIN ambition.callsummary b ON a.NOTABLECALLID = b.NOTABLECALLID
  WHERE b.ts between curdate() - interval 5 day and now()

  GROUP BY c.extension  
  ORDER BY round(sum(Duration) / 60,2) --Total_Talk_Time_minutes
 ) AS banana 
 OUTER APPLY (select count(*) as county, b.extension from banana b group by b.extension) AS [CallsPerExt]

Upvotes: 1

Related Questions