【问题标题】:Complicated select from one table从一张表中进行复杂的选择
【发布时间】:2021-03-03 07:47:53
【问题描述】:
-- I'm trying to create a report for Call Center with quality rating system.
-- Most of the calls are routed to the queues, where they wait to be answered by agents.
-- When finishing the call, agent can hangup, requeue the call to another queue
-- or requeue the call to the Rating queue, where caller may rate (or not)
-- the quality of service (e.g. 1..5)
-- There are also cases when calls are not answered, do not reach queue, etc.
-- Below is a simplified example table. 
-- Requeued call keeps the same callid, but requeuecount is increased by 1.
卡莉德 |队列 |代理|率 |重新排队计数 :----- | :-------- | :---- | ---: | ------------: 122 | |奥尔加 | | -1 123 | | | | -1 124 |销售 | | | 0 125 |服务 |汤姆 | | 0 126 |销售 |颜 | | 0 127 |销售 |金 | | 0 127 |服务 |约翰 | | 1 127 |评级 | | 4 | 2 128 |服务 |奥列格 | | 0 128 |评级 | | | 1 129 |服务 |约翰 | | 0 130 |服务 |约翰 | | 0 130 |评级 | | 2 | 1 131 |销售 |金 | | 0 131 |服务 |奥列格 | | 1 132 |服务 |奥列格 | | 0 132 |评级 | | 5 | 1
-- Those only agents who requeued calls to Rating queue.
-- queue name, agent name, number of calls requeued to Rating, 
-- number of rated calls, average rating

select cr.queue, cr.agent, count(cr.callid) as req2rating, 
count(cr1.rate) as ratingscount, avg(cr1.rate) as averagerating
from callrecord cr
inner join callrecord cr1 
on cr.callid=cr1.callid
and cr.requeuecount=cr1.requeuecount-1
and cr1.queue='Rating'
group by cr.queue, cr.agent
队列 |代理| req2rating |收视率|平均评分 :-------- | :---- | ---------: | ------------: | ------------: 服务 |约翰 | 2 | 2 | 3 服务 |奥列格 | 2 | 1 | 5
-- All agents with their number of received calls.
-- queue name, agent name, number of calls he have got

select queue, agent, count(callid) as totalcalls
from callrecord
group by queue, agent
队列 |代理|总通话 :-------- | :---- | ---------: | | 1 评级 | | 4 销售 | | 1 服务 |约翰 | 3 销售 |金 | 2 服务 |奥列格 | 3 |奥尔加 | 1 服务 |汤姆 | 1 销售 |颜 | 1
-- I want to combine the two above tables into one.
-- Straightforward method, that works:

select s1.queue, s1.agent, s1.totalcalls, s2.req2rating, s2.ratingscount, s2.averagerating from
  (select queue, agent, count(callid) as totalcalls
  from callrecord
  group by queue, agent) s1
left join
  (select cr.queue, cr.agent, count(cr.callid) as req2rating, 
  count(cr1.rate) as ratingscount, avg(cr1.rate) as averagerating
  from callrecord cr
  inner join callrecord cr1 
  on cr.callid=cr1.callid
  and cr.requeuecount=cr1.requeuecount-1 and cr1.queue='Rating'
  group by cr.queue, cr.agent) s2
on s1.queue=s2.queue and s1.agent=s2.agent
队列 |代理|总来电 | req2rating |收视率|平均评分 :-------- | :---- | ---------: | ---------: | ------------: | ------------: | | 1 | | | 评级 | | 4 | | | 销售 | | 1 | | | 服务 |约翰 | 3 | 2 | 2 | 3 销售 |金 | 2 | | | 服务 |奥列格 | 3 | 2 | 1 | 5 |奥尔加 | 1 | | | 服务 |汤姆 | 1 | | | 销售 |颜 | 1 | | |
-- Maybe there is a more clever and efficient way of doing this?
-- I mean using only one select operand.

db小提琴here

【问题讨论】:

    标签: sql count informix


    【解决方案1】:

    您可以选择 callrecords-agents 并以这种方式离开 callrecords-ratings:

    select cr.queue, cr.agent, count(cr.callid) as totalcalls,
           count(rt.callid) as req2rating,
           count(rt.rate) as ratingscount,
           avg(rt.rate) as averagerating
      from callrecord as cr
      left join callrecord as rt on
                rt.callid = cr.callid and
                rt.queue = 'Rating' and
                rt.requeuecount = cr.requeuecount+1
     group by cr.queue, cr.agent
    

    这会返回 req2rating 和 ratingcount 为 0,如果你想要 null 可以使用 nullif 函数:

    select cr.queue, cr.agent, count(cr.callid) as totalcalls,
           nullif(count(rt.callid), 0) as req2rating,
           nullif(count(rt.rate), 0) as ratingscount,
           avg(rt.rate) as averagerating
      from callrecord as cr
      left join callrecord as rt on
                rt.callid = cr.callid and
                rt.queue = 'Rating' and
                rt.requeuecount = cr.requeuecount+1
     group by cr.queue, cr.agent
    

    【讨论】:

    猜你喜欢
    • 2014-04-21
    • 1970-01-01
    • 2011-05-02
    • 1970-01-01
    • 1970-01-01
    • 2016-09-23
    • 1970-01-01
    • 1970-01-01
    • 2020-09-12
    相关资源
    最近更新 更多