with master_r08 as (select n.region
                         , m.date
                         , n.tech
                         , n.band
                         , r.branch
                         , r.kota_kabupaten
                         , n.site_id
                         , n.cell_name
                         , n.cell
                         , n.lac
                         , sum(tch_traffic_erlang)                                     as traffic_erl
                         , sum(payload_mbyte)                                          as payload_mbyte
                         , avg(num_tch_availability)                                   as num_availability
                         , avg(denum_tch_availability)                                 as denum_availability
                         , ndp(avg(num_tch_availability), avg(denum_tch_availability)) as availability
                    from r08.meas_2g_daily m
                             join network_cell n on m.network_cell_id = n.id
                             join ref_site r on n.site_id = r.site_id
                    where m.date between '2023-12-01' and '2023-12-07'
                     and n.site_id in ('BLC336')
                    group by n.region
                           , m.date
                           , n.tech
                           , n.band
                           , r.branch
                           , r.kota_kabupaten
                           , n.site_id
                           , n.cell_name
                           , n.cell
                           , n.lac

                    union all

                    select n.region
                         , m.date
                         , n.tech
                         , n.band
                         , r.branch
                         , r.kota_kabupaten
                         , n.site_id
                         , n.cell_name
                         , n.cell
                         , n.lac
                         , sum(volte_traffic_erl)                                        as traffic
                         , sum(payload_mbyte)                                            as payload_mbyte
                         , sum(cell_availability_num)                                    as num_availability
                         , sum(cell_availability_denum)                                  as denum_availability
                         , ndp(sum(cell_availability_num), sum(cell_availability_denum)) as availability
                    from r08.meas_4g_daily m
                             join network_cell n on m.network_cell_id = n.id
                             join ref_site r on n.site_id = r.site_id
                    where m.date between '2023-12-01' and '2023-12-07'
                     and n.site_id in ('BLC336')
                    group by n.region
                           , m.date
                           , n.tech
                           , n.band
                           , r.branch
                           , r.kota_kabupaten
                           , n.site_id
                           , n.cell_name
                           , n.cell
                           , n.lac

                    union all

                    select n.region
                         , m.date
                         , n.tech
                         , n.band
                         , r.branch
                         , r.kota_kabupaten
                         , n.site_id
                         , n.cell_name
                         , n.cell
                         , n.lac
                         , 0                                                   as traffic
                         , sum(payload)                                        as payload_mbyte
                         , sum(num_availability)                               as num_availability
                         , count(*) * 24 * 60 * 60                             as denum_availability
                         , ndp(sum(num_availability), count(*) * 24 * 60 * 60) as availability
                    from r08.meas_5g_daily m
                             join network_cell n on m.network_cell_id = n.id
                             join ref_site r on n.site_id = r.site_id
                    where m.date between '2023-12-01' and '2023-12-07'
                     and n.site_id in ('BLC336')
                    group by n.region
                           , m.date
                           , n.tech
                           , n.band
                           , r.branch
                           , r.kota_kabupaten
                           , n.site_id
                           , n.cell_name
                           , n.cell
                           , n.lac)
select region
     , tech
     , date
     , band
     , branch
     , kota_kabupaten
     , site_id
     , site_id
     , cell_name
     , cell
     , lac
     , traffic_erl::numeric(100, 2)        as traffic_erl
     , payload_mbyte::numeric(100, 2)      as payload_mbyte
     , num_availability::numeric(100, 2)   as num_availability
     , denum_availability::numeric(100, 2) as denum_availability
     , availability::numeric(100, 2)       as availability
from master_r08
order by region, tech, branch, kota_kabupaten, band, cell_name, date
;