﻿

-- b.tplg_descricao, tipo_chamada, a.uniqueid, a.calldate, a.src, a.dst, a.billsec, a.duration, a.userfield, case when(tplg_codigo in(23,24,33,34))then 'MÓVEL' else c.nome_municipio end as nome_municipio, c.uf
-- select * from pbx_tipo_ligacao
-- select * from pbx_municipios
-- select * from pbx_bilhetes where uniqueid = '1518188607.575'
-- select * from pbx_bilhetes_complemento where uniqueid2 = '1518188607.575'
-- select * from pbx_eventos_dacs where uid2 = '1517593767.4421'

select uniqueid, calldate, src, dst, tempo_atendimento, fila, agente, evento, nome_municipio, uf, tplg_descricao, status 

from (

select a.uniqueid, a.calldate, a.src, a.dst, case when(d.evento = 'ABANDON')then strtoint(d.param3) else a.billsec end as tempo_atendimento, coalesce(d.fila, '-') as fila, coalesce(e.apelido, '-') as agente,
       d.evento, case when(tplg_codigo in(23,24))then 'MÓVEL' else c.nome_municipio end as nome_municipio, c.uf, b.tplg_descricao,
       case when((d.evento in('COMPLETEAGENT','COMPLETECALLER') and (billsec > 3) ) or ((d.evento is null) and (billsec > 3)))then 'ATENDIDA' else 'N ATENDIDADA' end as status
  from pbx_bilhetes a
 inner join   pbx_tipo_ligacao b on b.tplg_codigo = a.tipo_chamada
 inner join  pbx_municipios c on c.id = a.id_municipio
 left join pbx_eventos_dacs d on d.uid2 = a.uniqueid and d.evento in('COMPLETEAGENT','COMPLETECALLER','ABANDON')
 left join pbx_usuarios e on e.matricula = substring(d.agente,7,4)
 where b.tplg_tipo = 2
 --  and a.data_bilhete >= '2018-02-01'
   --and a.data_bilhete <= '2018-02-10'
   and dst = 's'
   and a.lastapp <> 'Transferred Call'
) a

--Data/Hora 	Origem 	Destino 	Tempo 	Status 	Fila 	Municipio 	UF

select * from pbx_bilhetes where uniqueid in(
select * from  verifica_s );

select * from pbx_bilhetes_complemento   where uniqueid2 in(
select * from  verifica_s );

select a.*
  from pbx_bilhetes a
   inner join   pbx_tipo_ligacao b on b.tplg_codigo = a.tipo_chamada
 where b.tplg_tipo = 2 
   and a.data_bilhete >= '2018-02-01'
   and a.data_bilhete <= '2018-02-10'
   and d.billsec > 3
   and (dst = 's' or dst like 'a%')
   and a.lastapp <> 'Transferred Call'

