简体   繁体   English

来自子查询的mysql地址外部查询

[英]mysql address outer query from subquery

I have this query: 我有这个查询:

SELECT 
    sec_to_time(avg(t1.sessiontime)) as aloc,
    CONCAT(TRUNCATE(sum(t1.terminatecauseid = 1) * 100 / count(*),
                1),
            '%') as asr,
    count(*) as calls,
    cast(t1.destination as unsigned) as prefix,
    t2.destination as destination,
    SEC_TO_TIME(sum(t1.sessiontime)) as duration
FROM
    cc_call AS t1
        inner join
    cc_prefix as t2 ON t1.destination = t2.prefix
WHERE
    t1.card_id = '133' AND t1.starttime >= ('2014-06-1') AND t1.starttime <= ('2014-07-01 23:59:59') and t1.terminatecauseid = 1
group by t1.destination
order by duration DESC
LIMIT 0 , 25

t1.terminatecauseid = 1 means successful call, 'asr' means average success rate, t1.terminatecauseid = 1表示成功呼叫,“ asr”表示平均成功率,

Im trying to find out how many calls with (t1.terminatecauseid = 1) from the total calls made to an extension. 我试图从对扩展的总呼叫中找出多少个(t1.terminatecauseid = 1)的呼叫。

this line doesn't work: 该行不起作用:

sum(t1.terminatecauseid = 1) * 100 / count(*)

since I already have (t1.terminatecauseid = 1) in the WHERE clause. 因为我已经在WHERE子句中有了(t1.terminatecauseid = 1)。

Im thinking about putting a subquery, to retrieve total calls, where count(*) currently is. 我正在考虑放置一个子查询来检索总调用,当前count(*)在哪里。

How can I have this query calculate the ASR with total calls made? 我如何让此查询计算总调用次数的ASR?

example sqlfiddle 例子sqlfiddle

if possible, I'd like to not show results with duration=NULL 如果可能的话,我想没有表现出与时间= NULL结果

Use conditional aggregation, something like this: 使用条件聚合,如下所示:

SELECT sec_to_time(avg(case when t1.terminatecauseid = 1 then t1.sessiontime end)) as aloc,
       CONCAT(TRUNCATE(sum(t1.terminatecauseid = 1) * 100 / count(*),
               1),
             '%') as asr,
       count(*) as TotalCalls,
       sum(t1.terminatecauseid = 1) as Terminated1Calls,
       cast(t1.destination as unsigned) as prefix,
       t2.destination as destination,
       SEC_TO_TIME(sum(case when t1.terminatecauseid = 1 then t1.sessiontime end)) as duration
FROM cc_call t1 inner join
     cc_prefix t2
     ON t1.destination = t2.prefix
WHERE t1.card_id = '133' AND
      t1.starttime >= ('2014-06-1') AND t1.starttime <= ('2014-07-01 23:59:59') 
group by t1.destination
order by duration DESC
LIMIT 0 , 25;

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

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