Original query:

with relevant_rows AS (
    select setup_time, caller_number,clear_number,
           duration, cost, price, op_ips.pt_name as op_name from cdrs.mc_cdr
          left join speedflow.media_query.ips as op_ips
                    on op_ips.pt_id = cdrs.mc_cdr.pt_id and op_ips.is_op = true
where mc_cdr.setup_time >= (now() - interval '1 hour')
                             and mc_cdr.setup_time <= now()
                             and length(clear_number)>=(9)
                             and direction=1
                             and op_ips.pt_name like '%Orig_VAS' and caller_number != ''
), relevant_prefix as (
    select left(clear_number,-2) as number_prefix,
    max(setup_time) as last_setup_time,
    min(setup_time) as first_setup_time,
    count(*) as calls_count,
    count(*) filter (where duration > 0)   as calls_count_with_duration
        from relevant_rows --    WINDOW partials as (partition by left(clear_number,-2) order by setup_time  RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)
GROUP BY left(clear_number,-2)
) select * from relevant_prefix;