-- QUALITY
with master_08 as (select n.tech
                        , yearweek
                        , date
                        , kota_kabupaten
                        , band
                        , avg(average_cqi)                                                                 as average_cqi
                        , avg(spectral_efficiency)                                                         as spectral_efficiency
                        , sum(volte_traffic_erl)
                        , avg((4.15 * number_of_times_the_ul_emi_is_in_range_1_for_volte_users_times +
                               3.3 * number_of_times_the_ul_emi_is_in_range_2_for_volte_users_times +
                               2.3 * number_of_times_the_ul_emi_is_in_range_3_for_volte_users_times +
                               1.4 * number_of_times_the_ul_emi_is_in_range_4_for_volte_users_times) /
                              nullif((number_of_times_the_ul_emi_is_in_range_1_for_volte_users_times +
                                      number_of_times_the_ul_emi_is_in_range_2_for_volte_users_times +
                                      number_of_times_the_ul_emi_is_in_range_3_for_volte_users_times +
                                      number_of_times_the_ul_emi_is_in_range_4_for_volte_users_times), 0)) as ul_emi
                        , avg((4.15 * number_of_times_the_dl_emi_is_in_range_1_for_volte_users_times +
                               3.3 * number_of_times_the_dl_emi_is_in_range_2_for_volte_users_times +
                               2.3 * number_of_times_the_dl_emi_is_in_range_3_for_volte_users_times +
                               1.4 * number_of_times_the_dl_emi_is_in_range_4_for_volte_users_times) /
                              nullif((number_of_times_the_dl_emi_is_in_range_1_for_volte_users_times +
                                      number_of_times_the_dl_emi_is_in_range_2_for_volte_users_times +
                                      number_of_times_the_dl_emi_is_in_range_3_for_volte_users_times +
                                      number_of_times_the_dl_emi_is_in_range_4_for_volte_users_times), 0)) as dl_emi
                        , sum(volte_call_drop_rate_num)                                                    as call_drop_rate_num
                        , sum(volte_call_drop_rate_denum)                                                  as traffic_erl
                        , 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

                   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
                        , avg(average_cqi)                                                                 as average_cqi
                        , avg(spectral_efficiency)                                                         as spectral_efficiency
                        , sum(volte_traffic_erl)
                        , avg((4.15 * number_of_times_the_ul_emi_is_in_range_1_for_volte_users_times +
                               3.3 * number_of_times_the_ul_emi_is_in_range_2_for_volte_users_times +
                               2.3 * number_of_times_the_ul_emi_is_in_range_3_for_volte_users_times +
                               1.4 * number_of_times_the_ul_emi_is_in_range_4_for_volte_users_times) /
                              nullif((number_of_times_the_ul_emi_is_in_range_1_for_volte_users_times +
                                      number_of_times_the_ul_emi_is_in_range_2_for_volte_users_times +
                                      number_of_times_the_ul_emi_is_in_range_3_for_volte_users_times +
                                      number_of_times_the_ul_emi_is_in_range_4_for_volte_users_times), 0)) as ul_emi
                        , avg((4.15 * number_of_times_the_dl_emi_is_in_range_1_for_volte_users_times +
                               3.3 * number_of_times_the_dl_emi_is_in_range_2_for_volte_users_times +
                               2.3 * number_of_times_the_dl_emi_is_in_range_3_for_volte_users_times +
                               1.4 * number_of_times_the_dl_emi_is_in_range_4_for_volte_users_times) /
                              nullif((number_of_times_the_dl_emi_is_in_range_1_for_volte_users_times +
                                      number_of_times_the_dl_emi_is_in_range_2_for_volte_users_times +
                                      number_of_times_the_dl_emi_is_in_range_3_for_volte_users_times +
                                      number_of_times_the_dl_emi_is_in_range_4_for_volte_users_times), 0)) as dl_emi
                        , sum(volte_call_drop_rate_num)                                                    as call_drop_rate_num
                        , sum(volte_call_drop_rate_denum)                                                  as traffic_erl
                        , 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

                   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
                        , avg(average_cqi)                                                                 as average_cqi
                        , avg(spectral_efficiency)                                                         as spectral_efficiency
                        , sum(volte_traffic_erl)
                        , avg((4.15 * number_of_times_the_ul_emi_is_in_range_1_for_volte_users_times +
                               3.3 * number_of_times_the_ul_emi_is_in_range_2_for_volte_users_times +
                               2.3 * number_of_times_the_ul_emi_is_in_range_3_for_volte_users_times +
                               1.4 * number_of_times_the_ul_emi_is_in_range_4_for_volte_users_times) /
                              nullif((number_of_times_the_ul_emi_is_in_range_1_for_volte_users_times +
                                      number_of_times_the_ul_emi_is_in_range_2_for_volte_users_times +
                                      number_of_times_the_ul_emi_is_in_range_3_for_volte_users_times +
                                      number_of_times_the_ul_emi_is_in_range_4_for_volte_users_times), 0)) as ul_emi
                        , avg((4.15 * number_of_times_the_dl_emi_is_in_range_1_for_volte_users_times +
                               3.3 * number_of_times_the_dl_emi_is_in_range_2_for_volte_users_times +
                               2.3 * number_of_times_the_dl_emi_is_in_range_3_for_volte_users_times +
                               1.4 * number_of_times_the_dl_emi_is_in_range_4_for_volte_users_times) /
                              nullif((number_of_times_the_dl_emi_is_in_range_1_for_volte_users_times +
                                      number_of_times_the_dl_emi_is_in_range_2_for_volte_users_times +
                                      number_of_times_the_dl_emi_is_in_range_3_for_volte_users_times +
                                      number_of_times_the_dl_emi_is_in_range_4_for_volte_users_times), 0)) as dl_emi
                        , sum(volte_call_drop_rate_num)                                                    as call_drop_rate_num
                        , sum(volte_call_drop_rate_denum)                                                  as traffic_erl
                        , 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

                   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
     , average_cqi::numeric(100, 2)              as average_cqi
     , spectral_efficiency::numeric(100, 2)      as spectral_efficiency
     , ul_emi::numeric(100, 2)                   as ul_emi
     , dl_emi::numeric(100, 2)                   as dl_emi
     , call_drop_rate_num::numeric(100, 2)       as call_drop_rate_num
     , traffic_erl::numeric(100, 2)              as traffic_erl
     , 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
from combined
order by kota_kabupaten, band, date
;

