﻿select * from pbx_campanha;
select * from pbx_campanha_lista;
select * from pbx_campanha_contato;
select * from pbx_campanha_contato_fone;
select * from pbx_campanha_usuarios;

begin;
delete from pbx_campanha_contato_display where cmp_id = 131;
delete from pbx_campanha_contato_fone where cmp_id = 131;
delete from pbx_campanha_contato where cmp_id = 131;
delete from pbx_campanha_lista where cmp_id = 131;
delete from pbx_campanha_usuarios  where cmp_id = 131;
delete from pbx_campanha_operacao  where cmp_id = 131;
delete from pbx_campanha where cmp_id = 131;

--  rollback;
-- commit

select * 
  from pbx_campanha a, pbx_campanha_lista b, pbx_campanha_contato c, pbx_campanha_contato_fone d
 where b.cmp_id = a.cmp_id
   and c.list_id = b.list_id
   and d.cont_id = c.cont_id
   and a.cmp_id = 132;

   select b.nome, b.matricula, count(*) 
     from pbx_cliente a, pbx_usuarios b
    where b.matricula = a.client_user_contato
      and coalesce(client_user_contato, '') <> ''
     group by b.nome, b.matricula
     order by 1

delete from pbx_campanha_contato where  not exists(select '' from pbx_campanha_contato_fone where cont_id = pbx_campanha_contato.cont_id);
