创建查询以按 2 个字段获取未完成呼叫组的计数

Create query to get count of uncompleted calls group by 2 fields

这个问题的小更新

有tablewaiter_log为

+---------------------+---------+----------------+--------------+--------------+
| call_time           | call_id | queue_num_curr | ast_num_curr | proceed_wait |
+---------------------+---------+----------------+--------------+--------------+
| 2019-11-18 08:14:30 | f27de4f | 9010           | 2            |            1 |
| 2019-11-18 08:14:35 | f27de4f | 9002           | 5            |            1 |
| 2019-11-18 08:14:41 | f27de4f | 9003           | 1            |            0 |
| 2019-11-18 08:14:45 | asdf231 | 9010           | 2            |            1 |
| 2019-11-18 08:14:50 | asdf231 | 9002           | 5            |            1 |
| 2019-11-18 08:14:55 | rete125 | 9010           | 2            |            1 |
| 2019-11-18 08:15:00 | rete125 | 9009           | 5            |            1 |
| 2019-11-18 08:15:05 | a7rf5gs | 9003           | 2            |            1 |
| 2019-11-18 08:15:10 | a7rf5gs | 9006           | 5            |            1 |
| 2019-11-18 08:15:15 | a7rf5gs | 9009           | 1            |            0 |
| 2019-11-18 08:15:20 | qawe234 | 9003           | 2            |            1 |
| 2019-11-18 08:15:25 | qawe234 | 9008           | 5            |            1 |
| 2019-11-18 08:15:30 | qawe234 | 9004           | 1            |            0 |
| 2019-11-18 08:15:35 | 49c43ad | 9004           | 2            |            1 |
| 2019-11-18 08:15:41 | 49c43ad | 9007           | 5            |            1 |
| 2019-11-18 08:15:45 | bxfdrtr | 9010           | 3            |            1 |
| 2019-11-18 08:15:50 | bxfdrtr | 9012           | 4            |            1 |
| 2019-11-18 08:15:55 | tofnt62 | 9010           | 5            |            1 |
| 2019-11-18 08:16:00 | tofnt62 | 9021           | 1            |            1 |
+---------------------+---------+----------------+--------------+--------------+

呼叫 ID 'f27de4f' 的呼叫开始于 9010,结束于 9003,因为有一条记录 proceed_wait = 0 for call-id='f27de4f' Call with call-id 'asdf231' 从 9010 开始,仍然在 9002 进行,但尚未完成,因为没有记录 proceed_wait = 0 for call-id='asdf231' 与 call-id [=28 的呼叫类似=] 没有 proceed_wait = 0 的记录并且此调用也未完成。因此,对于队列 9010 查询结果应该是:

queue_num      ast_num  count 
9010            2       2
9010            3       1
9010            5       1

对于 9003 结果应该是 0 ,因为对 9003('a7rf5gs' 和 'qawe234')的所有调用都已完成。对于 9004,结果应为 1,因为对于呼叫 ID 为“49c43ad”的呼叫,没有 proceed_wait = 0 的记录。

所以结果应该是:

queue_num      ast_num  count 
9010            2       2
9010            3       1
9010            5       1
9004            2       1

您可以将 table 与检索未完成呼叫的最小 call_time par call_id 的聚合查询结合起来。未完成的通话是没有记录的通话 proceed_wait = 0.

select t.queue_num_curr, t.ast_num_curr, count(*)
from mytable t
inner join (
    select call_id, min(call_time) call_time
    from mytable 
    group by call_id
    having max(proceed_wait = 0) = 0
) tmin on tmin.call_id = t.call_id and tmin.call_time = t.call_time
group by t.queue_num_curr, t.ast_num_curr
order by t.queue_num_curr, t.ast_num_curr

Demo on DB Fiddle:

queue_num_curr | ast_num_curr | count(*)
-------------: | -----------: | -------:
          9004 |            2 |        1
          9010 |            2 |        2
          9010 |            3 |        1
          9010 |            5 |        1

注意:我认为在结果中,queue_num = 9004 应该有 ast_num = 2 而不是 1(应该对应于 call_id 49c43ad)。