-- RHI
with master_08 as (select n.tech
                        , yearweek
                        , date
                        , kota_kabupaten
                        , band
                        , sum(num_rrc_setup_sr) * sum(num_e_rab_setup_sr) * sum(s1_signal_connection_est_sr_num)                         as num_cssr
                        , sum(denum_rrc_setup_sr) * sum(denum_e_rab_setup_sr) * sum(s1_signal_connection_est_sr_denum)                   as denum_cssr
                        , nds(sum(num_rrc_setup_sr) * sum(num_e_rab_setup_sr) * sum(s1_signal_connection_est_sr_num),
                              sum(denum_rrc_setup_sr) * sum(denum_e_rab_setup_sr) * sum(s1_signal_connection_est_sr_denum))              as cssr

                        , sum(num_e_rab_drop_rate_percent)                                                                               as num_retainability
                        , sum(denum_e_rab_drop_rate_percent)                                                                             as denum_retainability
                        , nds(sum(denum_e_rab_drop_rate_percent) - sum(num_e_rab_drop_rate_percent), sum(denum_e_rab_drop_rate_percent)) as retainability

                        , avg(cell_dl_throughput_kbps)                                                                                   as cell_dl_throughput_kbps
                        , avg(cell_ul_throughput_kbps)                                                                                   as cell_ul_throughput_kbps
                        , avg(user_dl_throughput_kbps)                                                                                   as user_dl_throughput_kbps
                        , avg(user_ul_throughput_kbps)                                                                                   as user_ul_throughput_kbps

                        , sum(volte_call_drop_rate_num)                                                                                  as call_drop_rate_num
                        , sum(volte_call_drop_rate_denum)                                                                                as call_drop_rate_denum
                        , nds(sum(volte_call_drop_rate_num), sum(volte_call_drop_rate_denum))                                            as call_drop_rate

                        , sum(volte_call_drop_rate_mme_num)                                                                              as call_drop_rate_mme_num
                        , sum(volte_call_drop_rate_mme_denum)                                                                            as call_drop_rate_mme_denum
                        , nds(sum(volte_call_drop_rate_mme_num), sum(volte_call_drop_rate_mme_denum))                                    as call_drop_rate_mme

                        , sum(e_rab_setup_volte_qci1_num) + sum(e_rab_setup_volte_qci5_num)                                              as e_rab_setup_sr_qci_1_qci_5_num
                        , sum(e_rab_setup_volte_qci1_denum) + sum(e_rab_setup_volte_qci5_denum)                                          as e_rab_setup_sr_qci_1_qci_5_denum
                        , nds(sum(e_rab_setup_volte_qci1_num) + sum(e_rab_setup_volte_qci5_num),
                              sum(e_rab_setup_volte_qci1_denum) + sum(e_rab_setup_volte_qci5_denum))                                     as e_rab_setup_sr_qci_1_qci_5


                   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_rrc_setup_sr) * sum(num_e_rab_setup_sr) * sum(s1_signal_connection_est_sr_num)                         as num_cssr
                        , sum(denum_rrc_setup_sr) * sum(denum_e_rab_setup_sr) * sum(s1_signal_connection_est_sr_denum)                   as denum_cssr
                        , nds(sum(num_rrc_setup_sr) * sum(num_e_rab_setup_sr) * sum(s1_signal_connection_est_sr_num),
                              sum(denum_rrc_setup_sr) * sum(denum_e_rab_setup_sr) * sum(s1_signal_connection_est_sr_denum))              as cssr

                        , sum(num_e_rab_drop_rate_percent)                                                                               as num_retainability
                        , sum(denum_e_rab_drop_rate_percent)                                                                             as denum_retainability
                        , nds(sum(denum_e_rab_drop_rate_percent) - sum(num_e_rab_drop_rate_percent), sum(denum_e_rab_drop_rate_percent)) as retainability

                        , avg(cell_dl_throughput_kbps)                                                                                   as cell_dl_throughput_kbps
                        , avg(cell_ul_throughput_kbps)                                                                                   as cell_ul_throughput_kbps
                        , avg(user_dl_throughput_kbps)                                                                                   as user_dl_throughput_kbps
                        , avg(user_ul_throughput_kbps)                                                                                   as user_ul_throughput_kbps

                        , sum(volte_call_drop_rate_num)                                                                                  as call_drop_rate_num
                        , sum(volte_call_drop_rate_denum)                                                                                as call_drop_rate_denum
                        , nds(sum(volte_call_drop_rate_num), sum(volte_call_drop_rate_denum))                                            as call_drop_rate

                        , sum(volte_call_drop_rate_mme_num)                                                                              as call_drop_rate_mme_num
                        , sum(volte_call_drop_rate_mme_denum)                                                                            as call_drop_rate_mme_denum
                        , nds(sum(volte_call_drop_rate_mme_num), sum(volte_call_drop_rate_mme_denum))                                    as call_drop_rate_mme

                        , sum(e_rab_setup_volte_qci1_num) + sum(e_rab_setup_volte_qci5_num)                                              as e_rab_setup_sr_qci_1_qci_5_num
                        , sum(e_rab_setup_volte_qci1_denum) + sum(e_rab_setup_volte_qci5_denum)                                          as e_rab_setup_sr_qci_1_qci_5_denum
                        , nds(sum(e_rab_setup_volte_qci1_num) + sum(e_rab_setup_volte_qci5_num),
                              sum(e_rab_setup_volte_qci1_denum) + sum(e_rab_setup_volte_qci5_denum))                                     as e_rab_setup_sr_qci_1_qci_5

                   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_rrc_setup_sr) * sum(num_e_rab_setup_sr) * sum(s1_signal_connection_est_sr_num)                         as num_cssr
                        , sum(denum_rrc_setup_sr) * sum(denum_e_rab_setup_sr) * sum(s1_signal_connection_est_sr_denum)                   as denum_cssr
                        , nds(sum(num_rrc_setup_sr) * sum(num_e_rab_setup_sr) * sum(s1_signal_connection_est_sr_num),
                              sum(denum_rrc_setup_sr) * sum(denum_e_rab_setup_sr) * sum(s1_signal_connection_est_sr_denum))              as cssr

                        , sum(num_e_rab_drop_rate_percent)                                                                               as num_retainability
                        , sum(denum_e_rab_drop_rate_percent)                                                                             as denum_retainability
                        , nds(sum(denum_e_rab_drop_rate_percent) - sum(num_e_rab_drop_rate_percent), sum(denum_e_rab_drop_rate_percent)) as retainability

                        , avg(cell_dl_throughput_kbps)                                                                                   as cell_dl_throughput_kbps
                        , avg(cell_ul_throughput_kbps)                                                                                   as cell_ul_throughput_kbps
                        , avg(user_dl_throughput_kbps)                                                                                   as user_dl_throughput_kbps
                        , avg(user_ul_throughput_kbps)                                                                                   as user_ul_throughput_kbps

                        , sum(volte_call_drop_rate_num)                                                                                  as call_drop_rate_num
                        , sum(volte_call_drop_rate_denum)                                                                                as call_drop_rate_denum
                        , nds(sum(volte_call_drop_rate_num), sum(volte_call_drop_rate_denum))                                            as call_drop_rate

                        , sum(volte_call_drop_rate_mme_num)                                                                              as call_drop_rate_mme_num
                        , sum(volte_call_drop_rate_mme_denum)                                                                            as call_drop_rate_mme_denum
                        , nds(sum(volte_call_drop_rate_mme_num), sum(volte_call_drop_rate_mme_denum))                                    as call_drop_rate_mme

                        , sum(e_rab_setup_volte_qci1_num) + sum(e_rab_setup_volte_qci5_num)                                              as e_rab_setup_sr_qci_1_qci_5_num
                        , sum(e_rab_setup_volte_qci1_denum) + sum(e_rab_setup_volte_qci5_denum)                                          as e_rab_setup_sr_qci_1_qci_5_denum
                        , nds(sum(e_rab_setup_volte_qci1_num) + sum(e_rab_setup_volte_qci5_num),
                              sum(e_rab_setup_volte_qci1_denum) + sum(e_rab_setup_volte_qci5_denum))                                     as e_rab_setup_sr_qci_1_qci_5

                   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_cssr::numeric(100, 2)                         as num_cssr
     , denum_cssr::numeric(100, 2)                       as denum_cssr
     , cssr::numeric(100, 2)                             as cssr
     , num_retainability::numeric(100, 2)                as num_retainability
     , denum_retainability::numeric(100, 2)              as denum_retainability
     , retainability::numeric(100, 2)                    as retainability
     , cell_dl_throughput_kbps::numeric(100, 2)          as cell_dl_throughput_kbps
     , cell_ul_throughput_kbps::numeric(100, 2)          as cell_ul_throughput_kbps
     , user_dl_throughput_kbps::numeric(100, 2)          as user_dl_throughput_kbps
     , user_ul_throughput_kbps::numeric(100, 2)          as user_ul_throughput_kbps
     , call_drop_rate_num::numeric(100, 2)               as call_drop_rate_num
     , call_drop_rate_denum::numeric(100, 2)             as call_drop_rate_denum
     , call_drop_rate::numeric(100, 4)                   as call_drop_rate
     , call_drop_rate_mme_num::numeric(100, 2)           as call_drop_rate_mme_num
     , call_drop_rate_mme_denum::numeric(100, 2)         as call_drop_rate_mme_denum
     , call_drop_rate_mme::numeric(100, 4)               as call_drop_rate_mme
     , e_rab_setup_sr_qci_1_qci_5_num::numeric(100, 2)   as e_rab_setup_sr_qci_1_qci_5_num
     , e_rab_setup_sr_qci_1_qci_5_denum::numeric(100, 2) as e_rab_setup_sr_qci_1_qci_5_denum
     , e_rab_setup_sr_qci_1_qci_5::numeric(100, 2)       as e_rab_setup_sr_qci_1_qci_5
from combined
order by kota_kabupaten, band, date
;

