1
I have a table with several record of calls and I need to perform some conditions, to separate records.
I already use select to validate the information
SELECT  count(*) as qtdade_status_dia,
        "TB_BASE_ACIONAMENTO".contrato || "TB_BASE_ACIONAMENTO".telefone || "TB_BASE_ACIONAMENTO".data_registro AS contrato_telefone_data_registro,
        "TB_BASE_ACIONAMENTO".data_registro,
        "TB_BASE_ACIONAMENTO".contrato,
        "TB_BASE_ACIONAMENTO".telefone,
        "TB_BASE_ACIONAMENTO".tipo_discagem,
        "TB_BASE_ACIONAMENTO".status_telefonia
        FROM "TB_BASE_ACIONAMENTO"
        GROUP BY 
        contrato_telefone_data_registro,
        "TB_BASE_ACIONAMENTO".data_registro,
        "TB_BASE_ACIONAMENTO".contrato,
        "TB_BASE_ACIONAMENTO".telefone,
        "TB_BASE_ACIONAMENTO".tipo_discagem,
        "TB_BASE_ACIONAMENTO".status_telefonia
        HAVING COUNT(*) >=3
From this query which is actually a view I consult on it.
You would need to count the status_telefonia field along with the above rules to then perform a select similar to the one below:
 SELECT * FROM "public"."VW_BS_TESTE" 
 WHERE "status_telefonia" = ' MACHINE' 
 OR "status_telefonia" = ' INVALID NUMBER' 
 AND "qtdade_status_dia" > '5' 
 AND "status_telefonia" NOT LIKE '% HANDLED%'
 AND  "data_registro" >= '01/01/2017' 
 AND "data_registro" <= '23/02/2017' 
 AND "tipo_discagem" = 'OUTBOUND'
If you do not find the result in the above query would make the following query:
SELECT * FROM "public"."VW_BS_TESTE" 
WHERE "status_telefonia" = ' BUSY' 
AND "qtdade_status_dia" >= '3' 
AND "status_telefonia" NOT LIKE '% HANDLED%'
AND  "data_registro" >= '01/01/2017' 
AND "data_registro" <= '23/02/2017' 
AND "tipo_discagem" = 'OUTBOUND'
I would like to create a query that you do in a single process to speed up the process. Could someone help me ?

With UNION he recognizes only the first SELECT, outside that would also need to group Status_telefonia to perform the query before, would not have to use some kind of condition ?
– Manassés Trabalho