MySQL - 是一个子查询,这里需要什么?

时间:2014-08-28 10:06:20

标签: mysql select subquery percentage

我目前有这样的疑问:

SELECT 
  sec_to_time(avg(t1.sessiontime)) as aloc,
  count(*) as calls
FROM
  table1 AS t1
    inner join
  table2 as t2 ON t1.destination = t2.prefix
WHERE
   t1.card_id = '101' 
AND 
   t1.terminatecauseid = 1
group by t1.destination

示例结果:

enter image description here

'calls'数据绑定到't1.terminatecauseid = 1'(意味着只接听电话)

我想从总拨打电话中获得一定比例的已回答的来电。 没有条件的相同查询(t1.terminatecauseid = 1)将给我总调用次数。

我想知道添加另一个名为“平均成功率”的列的最佳方法是:     总通话*成功通话/ 100 子查询在这里需要什么?还是一个全新的,不同的查询?

1 个答案:

答案 0 :(得分:1)

SELECT 
  sec_to_time(avg(t1.sessiontime)) as aloc,
  sum(t1.terminatecauseid = 1) * 100 / count(*) as Average_Success_Rate,
  sum(t1.terminatecauseid = 1) as calls
FROM
  table1 AS t1
    inner join
  table2 as t2 ON t1.destination = t2.prefix
WHERE
   t1.card_id = '101' 
group by t1.destination