select a.* from vds_empresas a, tmp_vds_clientes b where b.cli_id = a.emp_id and emp_status = 0 order by a.emp_razao_social; select a.emp_id, b.emp_razao_social, count(*), max(chm_calldate::date) as max_date from vds_chamadas a, vds_empresas b where b.emp_id = a.emp_id and a.emp_id in( select a.emp_id from vds_empresas a, tmp_vds_clientes b where b.cli_id = a.emp_id ) group by a.emp_id, b.emp_razao_social order by 4; select * from vds_protcolos limit 10 select count(*) from vds_protcolos a where a.prt_proto_parceiro is not null and exists(select '' from tmp_proto_pesquisa where num_protocolo = a.prt_proto_parceiro);