with raw as (select datetime
                  , date
                  , site_id
                  , cell_name
                  , cell                                                          as ci
                  , nds(sum(cell_availability_num), sum(cell_availability_denum)) as avail
                  , case
                        when nds(sum(cell_availability_num),
                                 sum(cell_availability_denum)) > 95 then 1
                        else 0 end                                                   avail_flag

                  , sum(payload_mbyte)                                            as payload_mbyte
                  , case when sum(payload_mbyte) = 0 then 1 else 0 end               payload_mbyte_flag
             from r11.meas_4g_hourly raw
                      join network_cell n on raw.network_cell_id = n.id
             where raw.date between date_int(7) and date_int(1)
             group by datetime, date, site_id, cell_name, cell),
     daily as (select date
                    , site_id
                    , cell_name
                    , ci
                    , avg(avail)::numeric(100, 2)         as avail
                    , sum(payload_mbyte)::numeric(100, 2) as payload_mbyte
                    , sum(avail_flag)                     as count_avail_gt_95
                    , sum(payload_mbyte_flag)             as count_payload_mbyte_eq_0
               from raw
               group by date, site_id, cell_name, ci)
select *
from daily
order by site_id, cell_name, date
;

-----------2G
with raw as (select datetime
                  , date
                  , site_id
                  , cell_name
                  , lac                                                         as lac
                  , ci                                                          as ci
                  , nds(sum(num_tch_availability), sum(denum_tch_availability)) as avail
                  , case
                        when nds(sum(num_tch_availability),
                                 sum(denum_tch_availability)) > 95 then 1
                        else 0 end                                                 avail_flag
                  , sum(payload_mbyte)                                          as payload_mbyte
                  , case when sum(payload_mbyte) = 0 then 1 else 0 end             payload_mbyte_flag
             from r11.meas_2g_hourly raw
                      join network_cell n on raw.network_cell_id = n.id
             where raw.date between date_int(7) and date_int(1)
             group by datetime, date, site_id, cell_name, lac, ci),
     daily as (select date
                    , site_id
                    , cell_name
                    , lac
                    , ci
                    , avg(avail)::numeric(100, 2)         as avail
                    , sum(payload_mbyte)::numeric(100, 2) as payload_mbyte
                    , sum(avail_flag)                     as count_avail_gt_95
                    , sum(payload_mbyte_flag)             as count_payload_mbyte_eq_0
               from raw
               group by date, site_id, cell_name, lac, ci)
select *
from daily
order by site_id, cell_name, date
;
