-- Capacity
with master_08 as (select n.tech
                        , yearweek
                        , date
                        , kota_kabupaten
                        , band

                        , sum(num_dl_prb_utilization_percent)                                             as num_dl_prb_utilization
                        , sum(denum_dl_prb_utilization_percent)                                           as denum_dl_prb_utilization
                        , nds(sum(num_dl_prb_utilization_percent), sum(denum_dl_prb_utilization_percent)) as dl_prb_utilization

                        , sum(num_ul_prb_utilization_percent)                                             as num_ul_prb_utilization
                        , sum(denum_ul_prb_utilization_percent)                                           as denum_ul_prb_utilization
                        , nds(sum(num_ul_prb_utilization_percent), sum(denum_ul_prb_utilization_percent)) as ul_prb_utilization


                   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 date between '2023-12-01' and '2023-12-07'
                   group by n.tech
                          , yearweek
                          , date
                          , kota_kabupaten
                          , band),
     master_09 as (select n.tech
                        , yearweek
                        , date
                        , kota_kabupaten
                        , band

                        , sum(num_dl_prb_utilization_percent)                                             as num_dl_prb_utilization
                        , sum(denum_dl_prb_utilization_percent)                                           as denum_dl_prb_utilization
                        , nds(sum(num_dl_prb_utilization_percent), sum(denum_dl_prb_utilization_percent)) as dl_prb_utilization

                        , sum(num_ul_prb_utilization_percent)                                             as num_ul_prb_utilization
                        , sum(denum_ul_prb_utilization_percent)                                           as denum_ul_prb_utilization
                        , nds(sum(num_ul_prb_utilization_percent), sum(denum_ul_prb_utilization_percent)) as ul_prb_utilization

                   from r09.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 date between '2023-12-01' and '2023-12-07'
                   group by n.tech
                          , yearweek
                          , date
                          , kota_kabupaten
                          , band),
     master_11 as (select n.tech
                        , yearweek
                        , date
                        , kota_kabupaten
                        , band

                        , sum(num_dl_prb_utilization_percent)                                             as num_dl_prb_utilization
                        , sum(denum_dl_prb_utilization_percent)                                           as denum_dl_prb_utilization
                        , nds(sum(num_dl_prb_utilization_percent), sum(denum_dl_prb_utilization_percent)) as dl_prb_utilization

                        , sum(num_ul_prb_utilization_percent)                                             as num_ul_prb_utilization
                        , sum(denum_ul_prb_utilization_percent)                                           as denum_ul_prb_utilization
                        , nds(sum(num_ul_prb_utilization_percent), sum(denum_ul_prb_utilization_percent)) as ul_prb_utilization

                   from r11.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 date between '2023-12-01' and '2023-12-07'
                   group by n.tech
                          , yearweek
                          , date
                          , kota_kabupaten
                          , band),
     combined as (select *
                  from master_08
                  union all
                  select *
                  from master_09
                  union all
                  select *
                  from master_11)

select tech
     , yearweek
     , date
     , kota_kabupaten
     , band
     , num_dl_prb_utilization::numeric(100, 2)   as num_dl_prb_utilization
     , denum_dl_prb_utilization::numeric(100, 2) as denum_dl_prb_utilization
     , dl_prb_utilization::numeric(100, 2)       as dl_prb_utilization
     , num_ul_prb_utilization::numeric(100, 2)   as num_ul_prb_utilization
     , denum_ul_prb_utilization::numeric(100, 2) as denum_ul_prb_utilization
     , ul_prb_utilization::numeric(100, 2)       as ul_prb_utilization
from combined
order by kota_kabupaten, band, date
;

