with master as (select '2G'                                                        as tech
                     , m.datetime
                     , m.date
                     , n.site_id
                     , n.cell_name
                     , n.lac
                     , n.ci

                     , ndp(sum(num_tch_availability), sum(denum_tch_availability)) as availability
                     , case
                           when ndp(sum(num_tch_availability), sum(denum_tch_availability)) > 95 then 1
                           else 0 end                                              as avail_flag_95
                     , case
                           when ndp(sum(num_tch_availability), sum(denum_tch_availability)) > 90 then 1
                           else 0 end                                              as avail_flag_90
                     , sum(tch_traffic_erlang)                                     as traffic
                     , case when sum(tch_traffic_erlang) = 0 then 1 else 0 end     as traffic_flag
                     , sum(payload_mbyte)                                          as payload_mbyte
                     , case when sum(payload_mbyte) = 0 then 1 else 0 end          as payload_mbyte_flag
                     , avg(data_integrity)                                         as data_integrity
                from r11.meas_2g_hourly m
                         join network_cell n on m.network_cell_id = n.id
                where date between '2023-12-01' and current_date
                  and n.site_id in
                      ('COK119', 'JAP032', 'JAP187', 'KRG001', 'KRG003', 'KRG004', 'MUL001', 'MUL006', 'MUL053', 'SMI008', 'SMI024', 'SMI013', 'SMI015', 'SMI016', 'WAM010', 'WAM012', 'WAM020', 'BIA012', 'BIA040',
                       'BTW002', 'BTW003', 'ENT008', 'ENT010', 'SMI007', 'JAP051', 'JAP031', 'WRS008', 'JAP022', 'JAP115', 'WRS005', 'JAP011', 'JAP013', 'JAP045', 'JAP047', 'JAP054', 'JAP009', 'WRS002', 'JAP041',
                       'WRS003', 'JAP008', 'JAP070', 'WRS004', 'WAM001', 'JAP060', 'JAP040', 'WAM007', 'JAP090', 'SMI004', 'JAP028', 'WRS001', 'JAP015', 'WAM002', 'WRS009', 'JAP646', 'SMI129', 'SMI128', 'SMI131',
                       'SMI040')
                group by m.datetime
                       , m.date
                       , n.site_id
                       , n.cell_name
                       , n.lac
                       , n.ci

                union

                select '4G'                                                          as tech
                     , m.datetime
                     , m.date
                     , n.site_id
                     , n.cell_name
                     , n.lac
                     , n.ci                                                          as ci

                     , ndp(sum(cell_availability_num), sum(cell_availability_denum)) as availability
                     , case
                           when ndp(sum(cell_availability_num), sum(cell_availability_denum)) > 95 then 1
                           else 0 end                                                as avail_flag_95
                     , case
                           when ndp(sum(cell_availability_num), sum(cell_availability_denum)) > 90 then 1
                           else 0 end                                                as avail_flag_90
                     , sum(volte_traffic_erl)                                        as traffic
                     , case when sum(volte_traffic_erl) = 0 then 1 else 0 end        as traffic_flag
                     , sum(payload_mbyte)                                            as payload_mbyte
                     , case when sum(payload_mbyte) = 0 then 1 else 0 end            as payload_mbyte_flag
                     , avg(data_integrity)                                           as data_integrity
                from r11.meas_4g_hourly m
                         join network_cell n on m.network_cell_id = n.id
                where date between '2023-12-01' and current_date
                  and n.site_id in
                      ('COK119', 'JAP032', 'JAP187', 'KRG001', 'KRG003', 'KRG004', 'MUL001', 'MUL006', 'MUL053', 'SMI008', 'SMI024', 'SMI013', 'SMI015', 'SMI016', 'WAM010', 'WAM012', 'WAM020', 'BIA012', 'BIA040',
                       'BTW002', 'BTW003', 'ENT008', 'ENT010', 'SMI007', 'JAP051', 'JAP031', 'WRS008', 'JAP022', 'JAP115', 'WRS005', 'JAP011', 'JAP013', 'JAP045', 'JAP047', 'JAP054', 'JAP009', 'WRS002', 'JAP041',
                       'WRS003', 'JAP008', 'JAP070', 'WRS004', 'WAM001', 'JAP060', 'JAP040', 'WAM007', 'JAP090', 'SMI004', 'JAP028', 'WRS001', 'JAP015', 'WAM002', 'WRS009', 'JAP646', 'SMI129', 'SMI128', 'SMI131',
                       'SMI040')
                group by m.datetime
                       , m.date
                       , n.site_id
                       , n.cell_name
                       , n.lac
                       , n.ci),
     daily as (select tech
                    , date
                    , site_id
                    , cell_name
                    , lac
                    , ci
                    , avg(availability)::numeric(100, 2)  as avail
                    , sum(traffic)::numeric(100, 2)       as traffic
                    , sum(payload_mbyte)::numeric(100, 2) as payload_mbyte
                    , sum(avail_flag_95)                  as count_avail_gt_95
                    , sum(avail_flag_90)                  as count_avail_gt_90
                    , sum(traffic_flag)                   as count_traffic_eq_0
                    , sum(payload_mbyte_flag)             as count_payload_mbyte_eq_0
                    , avg(data_integrity)                 as data_integrity
               from master
               group by tech
                      , date
                      , site_id
                      , cell_name
                      , lac
                      , ci)

select *
from daily
order by tech, cell_name, date
;