简体   繁体   English

MySQL中自定义公式的语法

[英]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. 我有一个可以正常工作的mysql查询,但我需要向其中添加三个平均值/百分比公式。 This query is for call center metrics. 该查询用于呼叫中心指标。

My real issue is in my third metric, missed_call_score . 我真正的问题是我的第三个指标missed_call_score I need to do something like this: 我需要做这样的事情:

(missed calls / total talk time) * (average calls per CSR / total calls ) * 100 . (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. 现在,我在下面的查询中错过了通话,通话总时间和通话总次数,但是为了获得每个csr的平均通话次数,我需要取出最高和最低的总通话次数(在15个CSR中),因此我m剩余13个。 I need to add those totals and I assume divide by 13, which would give me the average per CSR after removing highest/lowest. 我需要将这些总数相加,并假定除以13,这将在除去最高/最低值后得出每个CSR的平均值。

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. 因此,我需要将我的三个指标添加到我留下评论的select语句中。 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

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]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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