尝试从这两个查询中进行单个查询

时间:2019-07-11 19:53:25

标签: sql amazon-redshift

我正在尝试获取符合允许的时间范围的通话百分比。但是由于一种情况,通话时间少于30秒,我无法正常工作。我尝试在Select语句中算出30秒的条件,但这种方法不起作用(我每次都保持100%的效率,并且逐一查看数字后,这是不可能的。

Select date, count("speed of answer" < '00:00:30')/ count(calls) as SLA
From five9_data.calllog
Where "call type" = 'Inbound' and campaign in ('Eves Addiction', 'Brook and York') and "service level" = '1' and skill = 'Eves Sales V' 
Group By date
Order By date desc
Limit 5000

以下是全部2条查询:

Select date, count(calls) as Total
From five9_data.calllog
Where 
  "call type" = 'Inbound' 
  and campaign in ('Eves Addiction', 'Brook and York') 
  and "service level" = '1' 
  and skill = 'Eves Sales V'   
Group By date
Order By date desc

AND

Select date, count("speed of answer") as AnsweredInTime
From five9_data.calllog
Where 
  "call type" = 'Inbound' 
  and campaign in ('Eves Addiction', 'Brook and York') 
  and "service level" = '1' 
  and skill = 'Eves Sales V' 
  and "speed of answer" < '00:00:30'
Group By date
Order By date desc

它具有相同的数据源,因此union无效,并且认为Join无效。

结束游戏,我希望能够进行一个查询,使上面的2个查询能够起作用,并最终将AnsweredInTime除以Total。

3 个答案:

答案 0 :(得分:1)

COUNT(exp)函数将计算exp不为空的行数-不在乎true或false。您可以通过执行命令select count(false)(评估为1)和select count(NULL)(评估为0)来验证这一点。因此,而不是

 count("speed of answer" < '00:00:30')

您可以尝试

 count(nullif("speed of answer" < '00:00:30', FALSE))

(如果速度<00:30,则该值不会为null,因此将被计算在内)或

 sum(case when "speed of answer" < '00:00:30' then 1 else 0 end)

答案 1 :(得分:1)

我建议将查询编写为:

Select date,
       count(*) as total_count,
       sum( ("speed of answer" < '00:00:30')::int ) as num_AnsweredInTime,
       avg( ("speed of answer" < '00:00:30')::int ) as ratio_AnsweredInTime
from five9_data.calllog
where "call type" = 'Inbound' and
      campaign in ('Eves Addiction', 'Brook and York') and
      "service level" = '1' and
      skill = 'Eves Sales V'
Group By date
Order By date desc

答案 2 :(得分:0)

选择日期,        count(*)为total_count,        sum((“ answer speed” <'00:00:30'):: int)为num_AnsweredInTime,        avg((“ answer speed” <'00:00:30'):: int)为ratio_AnsweredInTime 从Five9_data.calllog 其中“呼叫类型” =“入站”和       广告系列(“除夕瘾”,“布鲁克与约克”)和       “服务水平” =“ 1”,并且       技能=“前夕销售V” 按日期分组 按日期排序