在呼叫前24小时查询以验证号码

时间:2016-12-09 12:25:26

标签: postgresql

我有一张像这样的表[POSTGRE] ......   - 接听电话的代理人,
  - 电话号码
  - 日期时间结束。

Agent   | Caller     |    DateTime   
------------------------------------
 101    | 555-1234  | 12-01-16 00:00
 101    | 555-1234  | 12-01-16 01:00 
 102    | 555-1234  | 12-01-16 02:00
 102    | 555-1234  | 13-01-16 06:00

1º - 我需要验证每个号码(每一行)..并在通话前24小时查看,以检查是否是召回,例如:第13天没有召回,因为此号码的最后一次通话不是在24小时内

2º - 需要召回代理人。

需要像这样显示......

Agent   | Caller     |    DateTime   | Recalling |  Recalling-From
----------------------------------------------------------------------
 101    | 555-1234  | 12-01-16 00:00 | NO        | 
 101    | 555-1234  | 12-01-16 01:00 | YES       | 101
 102    | 555-1234  | 12-01-16 02:00 | YES       | 101
 102    | 555-1234  | 13-01-16 06:00 | NO        | 

我的查询是......

SELECT 
to_char(datahora_entrada_fila, 'DD/MM/YYYY') as dia,
calleridnum As numero,
MAX(datahora_inicio) AS data_ini
FROM callcenter.chamada_fila_in cfin
WHERE datahora_inicio BETWEEN '2016-12-01 00:00:00' AND '2016-12-01 23:59:59'
AND status_chamada  = 'Finalizada'
GROUP BY dia,numero

)

SELECT 
to_char(datahora_entrada_fila, 'DD/MM/YYYY') as dia,
(SELECT count(calleridnum) FROM callcenter.chamada_fila_in f INNER JOIN maiordata md ON f.calleridnum = md.numero WHERE calleridnum = cfin.calleridnum AND datahora_inicio BETWEEN md.data_ini - interval '24 hours' AND md.data_ini ) AS QtdRechamada,
calleridnum As numero
FROM callcenter.chamada_fila_in cfin
WHERE datahora_inicio BETWEEN '2016-12-01 00:00:00' AND '2016-12-01 23:59:59'
AND status_chamada  = 'Finalizada'
GROUP BY dia,numero
ORDER BY
dia,numero DESC

需要更好的功能或方法.. 这个查询在我的数据库中非常繁重,需要进行优化。

0 个答案:

没有答案