﻿select b.fila, cast(a.calldate as date) as data,  date_part('hour', calldate) as hora, a.src as origem, count(*) as qtde, sum(param3::integer) as tempo
              from pbx_bilhetes a, pbx_eventos_dacs b
             where b.uid2 = a.uniqueid
               and b.evento = 'ABANDON'
               and a.lastapp <> 'Transferred Call'
               and cast(a.calldate as date) >= '2011-08-10'
               and cast(a.calldate as date) <= '2011-08-10'
               and b.fila = 'SefazNivel1'-- (select nome from pbx_dacs where id = '64')
            group by b.fila, cast(a.calldate as date),  date_part('hour', calldate), a.src
            order by 1, 2


           --select * from pbx_dacs "SefazNivel1"


           select a.dac_padrao from pbx_usuarios a, pbx_dacs b where b.id = a.dac_padrao and  a.id = 234

           select dac_padrao, * from pbx_usuarios where dac_padrao is null

           select * from  pbx_dac_usuario where id_usuario in(select id from pbx_usuarios)

           select a.dac_padrao 
		begin
           update pbx_usuarios
              set dac_padrao = b.id_dac
             from pbx_usuarios a, pbx_dac_usuario b
           where b.id_usuario = a.id
             and b.padrao = true
             and a.dac_padrao is null

             rollback

             select *
              from pbx_usuarios a, pbx_dac_usuario b
           where b.id_usuario = a.id
              and b.padrao = true
             and a.dac_padrao is null

          
           