with master as (select m.date
                     , r.kota_kabupaten
                     , r.branch
                     , r.rtpo

                     , nds(sum(num_rrc_setup_sr), sum(denum_rrc_setup_sr))                                                                                           as rrc_setup_sr
                     , sum(num_rrc_setup_sr)                                                                                                                         as num_rrc_setup_sr
                     , sum(denum_rrc_setup_sr)                                                                                                                       as denum_rrc_setup_sr

                     , 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_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_e_rab_setup_sr), sum(denum_e_rab_setup_sr))                                                                                       as e_rab_setup_sr
                     , sum(num_e_rab_setup_sr)                                                                                                                       as num_e_rab_setup_sr
                     , sum(denum_e_rab_setup_sr)                                                                                                                     as denum_e_rab_setup_sr

                     , nds(sum(num_e_rab_drop_rate_percent), sum(denum_e_rab_drop_rate_percent))                                                                     as e_rab_drop_rate
                     , sum(num_e_rab_drop_rate_percent)                                                                                                              as num_e_rab_drop_rate
                     , sum(denum_e_rab_drop_rate_percent)                                                                                                            as denum_e_rab_drop_rate

                     , nds(sum(ratio_of_rrc_re_establishment_monitor_num), sum(ratio_of_rrc_re_establishment_monitor_denum))                                         as ratio_of_rrc_re_establishment

                     , nds(sum(num_ifho_sr), sum(denum_ifho_sr))                                                                                                     as ifho_sr
                     , sum(num_ifho_sr)                                                                                                                              as num_ifho_sr
                     , sum(denum_ifho_sr)                                                                                                                            as denum_ifho_sr

                     , nds(sum(num_csfb_sr), sum(denum_csfb_sr))                                                                                                     as csfb_sr
                     , sum(num_csfb_sr)                                                                                                                              as num_csfb_sr
                     , sum(denum_csfb_sr)                                                                                                                            as denum_csfb_sr

                     , sum(number_of_rrc_connection_releases_triggered_by_csfb_times)                                                                                as number_of_rrc_connection_releases_triggered_by_csfb_times
                     , sum(number_of_redirection_requests_from_lte_to_utran_csfb_times)                                                                              as lte_number_of_redirection_requests_from_lte_to_utran_csfb_ranq
                     , sum(number_of_redirection_requests_from_lte_to_gsm_csfb_times)                                                                                as lte_number_of_redirection_requests_from_lte_to_gsm_csfb_ranq

                     , nds(sum(s1_signal_connection_est_sr_num), sum(s1_signal_connection_est_sr_denum))                                                             as s1_signaling_sr

                     , sum(dl_traffic_volume_mbyte)                                                                                                                  as dl_traffic_volume_mbyte
                     , sum(ul_traffic_volume_mbyte)                                                                                                                  as ul_traffic_volume_mbyte
                     , sum(payload_mbyte)                                                                                                                            as payload_mbyte

                     , sum(maximum_number_of_rrc_connection_user_unit)                                                                                               as maximum_number_of_rrc_connection_user
                     , sum(license_number_of_rrc_connection_user_cell_unit)                                                                                          as license_number_of_rrc_connection_user

                     , nds(sum(num_dl_prb_utilization_percent), sum(denum_dl_prb_utilization_percent))                                                               as dl_prb_utilization
                     , nds(sum(num_ul_prb_utilization_percent), sum(denum_ul_prb_utilization_percent))                                                               as ul_prb_utilization

                     , nds(sum(cell_availability_num), sum(cell_availability_denum))                                                                                 as cell_availability
--          , nds(sum(cell_availability_num), (count(*) * (60 * 60))::double precision)                             as cell_availability

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

                     , nds(sum(dl_64qam_modulation_scheme_usage_num), sum(dl_64qam_modulation_scheme_usage_denum))                                                   as dl_64qam_modulation_scheme_usage
                     , nds(sum(ul_16qam_modulation_scheme_usage_num), sum(ul_16qam_modulation_scheme_usage_denum))                                                   as ul_16qam_modulation_scheme_usage

                     , nds(sum(the_ratio_of_cqi_ge_7_monitor_num), sum(the_ratio_of_cqi_ge_7_monitor_denum))                                                         as the_ratio_of_cqi_ge_7_monitor

                     , nds(sum(prb_number_used_on_downlink_channel_unit), sum(prb_number_available_on_downlink_channel_unit))                                        as dl_resource_block_utilizing_rate
                     , nds(sum(prb_number_used_on_uplink_channel_unit), sum(prb_number_available_on_uplink_channel_unit))                                            as ul_resource_block_utilizing_rate


                     , nds(sum(cce_number_used_on_pdcch_channel_unit), sum(cce_number_available_on_pdcch_channel_unit))                                              as pdcch_cce_utilization_rate
                     , nds(sum(prb_number_used_on_pusch_channel_unit), sum(prb_number_available_on_pusch_channel_unit))                                              as pusch_prb_utilization_rate
                     , nds((sum(number_of_successful_access_using_group_a_preamble_unit) +
                            sum(number_of_successful_access_using_group_b_preamble_unit) +
                            sum(number_of_successful_access_using_dedicated_preamble_unit)),
                           sum(number_of_available_preamble_unit))                                                                                                   as prach_usage

                     , nds(sum(dl_qpsk_modulation_scheme_usage_num), sum(dl_qpsk_modulation_scheme_usage_denum))                                                     as dl_qpsk_modulation_scheme_usage
                     , nds(sum(dl_16qam_modulation_scheme_usage_num), sum(dl_16qam_modulation_scheme_usage_denum))                                                   as dl_16qam_modulation_scheme_usage
                     , nds(sum(dl_qpsk_modulation_scheme_usage_num), sum(ul_qpsk_modulation_scheme_usage_denum))                                                     as ul_qpsk_modulation_scheme_usage
                     , nds(sum(ul_64qam_modulation_scheme_usage_num), sum(ul_64qam_modulation_scheme_usage_denum))                                                   as ul_64qam_modulation_scheme_usage

                     , sum(number_of_paging_records_received_by_the_enodeb_unit)                                                                                     as paging_number_from_s1

                     , nds(sum(number_of_paging_records_congested_at_the_enodeb_unit),
                           sum(number_of_paging_records_received_by_the_enodeb_unit) + sum(number_of_paging_records_congested_at_the_enodeb_unit))                   as paging_congestion_rate
                     , nds(sum(number_of_paging_records_received_by_the_enodeb_unit),
                           sum(number_of_paging_records_send_from_enodeb_unit))                                                                                      as paging_success_rate

                     , avg(active_users_rnp)                                                                                                                         as active_users_rnp

                     , nds(
            sum(dl_256qam_modulation_scheme_usage_num),
            sum(dl_256qam_modulation_scheme_usage_denum))                                                                                                            as dl_256qam_modulation_scheme_usage

                     , avg(average_cqi)                                                                                                                              as average_cqi
                     , avg(cqi_ge_10)                                                                                                                                as cqi_ge_10
                     , avg(spectral_efficiency)                                                                                                                      as spectral_efficiency

                     , nds(sum(inter_freq_lte_ho_num), sum(inter_freq_lte_ho_denum))                                                                                 as inter_freq_lte_ho
                     , sum(inter_freq_lte_ho_denum)                                                                                                                  as inter_freq_lte_ho_denum
                     , sum(inter_freq_lte_ho_num)                                                                                                                    as inter_freq_lte_ho_num

                     , sum(num_ifho_sr)                                                                                                                              as num_ifho_sr_
                     , sum(denum_ifho_sr)                                                                                                                            as denum_ifho_sr_

                     , avg(cell_dl_tcp_packets_average_rtt_ms)                                                                                                       as cell_dl_tcp_packets_average_rtt

                     , avg(kpi_average_dl_pdcp_sdu_delay_ms)                                                                                                         as average_dl_pdcp_sdu_delay
                     , avg(kpi_average_dl_ip_packet_delay_ms)                                                                                                        as average_dl_ip_packet_delay
                     , avg(avgerage_delay_of_tcp_three_way_handshake_syn_to_synack_ms)                                                                               as average_delay_of_tcp_three_way_handshake_syn_to_synack
                     , avg(avgerage_delay_of_tcp_three_way_handshake_synack_to_ack_ms)                                                                               as average_delay_of_tcp_three_way_handshake_synack_to_ack
                     , avg(avgerage_delay_of_tcp_three_way_handshake_ack_to_firstdata_ms)                                                                            as average_delay_of_tcp_three_way_handshake_ack_to_firstdata

                     , avg(average_ni_of_carrier_dbm)                                                                                                                as average_ni_of_carrier_dbm
-- 
                     , nds(sum(rrc_establishment_volte_num) * sum(e_rab_setup_volte_qci5_num),
                           sum(rrc_establishment_volte_denum) * sum(e_rab_setup_volte_qci5_denum))                                                                   as call_setup_sr_qci_1_qci_5
                     , sum(rrc_establishment_volte_num) * sum(e_rab_setup_volte_qci5_num)                                                                            as call_setup_sr_qci_1_qci_5_num
                     , sum(rrc_establishment_volte_denum) * sum(e_rab_setup_volte_qci5_denum)                                                                        as call_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
                     , 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(volte_call_drop_rate_mme_num), sum(volte_call_drop_rate_mme_denum))                                                                   as call_drop_rate_mme
                     , 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_num), sum(volte_call_drop_rate_denum))                                                                           as call_drop_rate
                     , sum(volte_call_drop_rate_num)                                                                                                                 as call_drop_rate_num
                     , sum(volte_call_drop_rate_denum)                                                                                                               as call_drop_rate_denum

                     , sum(volte_traffic_erl)                                                                                                                        as traffic_erl
                     , sum(maximum_dl_active_user_number_qci_1_unit)                                                                                                 as maximum_dl_active_user_number_qci_1

                     , nds(sum(volte_user_outgoing_inter_freq_hosr_num), sum(volte_user_outgoing_inter_freq_hosr_denum))                                             as user_outgoing_inter_freq_hosr
                     , sum(volte_user_outgoing_inter_freq_hosr_num)                                                                                                  as user_outgoing_inter_freq_hosr_num
                     , sum(volte_user_outgoing_inter_freq_hosr_denum)                                                                                                as user_outgoing_inter_freq_hosr_denum

                     , nds(sum(volte_user_outgoing_intra_freq_hosr_num), sum(volte_user_outgoing_intra_freq_hosr_denum))                                             as user_outgoing_intra_freq_hosr
                     , sum(volte_user_outgoing_intra_freq_hosr_num)                                                                                                  as user_outgoing_intra_freq_hosr_num
                     , sum(volte_user_outgoing_intra_freq_hosr_denum)                                                                                                as user_outgoing_intra_freq_hosr_denum

                     , nds(sum(volte_srvcc_ho_sr_lte_to_umts_success_div_success_failure_num), sum(volte_srvcc_ho_sr_lte_to_umts_success_div_success_failure_denum)) as srvcc_hosr_lte_umts_succ_div_succ_plus_fail
                     , sum(volte_srvcc_ho_sr_lte_to_umts_success_div_success_failure_num)                                                                            as srvcc_hosr_lte_umts_succ_div_succ_plus_fail_num
                     , sum(volte_srvcc_ho_sr_lte_to_umts_success_div_success_failure_denum)                                                                          as srvcc_hosr_lte_umts_succ_div_succ_plus_fail_denum

                     , nds(sum(volte_srvcc_ho_sr_lte_to_gsm_success_div_success_failure_num), sum(volte_srvcc_ho_sr_lte_to_gsm_success_div_success_failure_denum))   as srvcc_hosr_lte_gsm_succ_div_succ_plus_fail
                     , sum(volte_srvcc_ho_sr_lte_to_gsm_success_div_success_failure_num)                                                                             as srvcc_hosr_lte_gsm_succ_div_succ_plus_fail_num
                     , sum(volte_srvcc_ho_sr_lte_to_gsm_success_div_success_failure_denum)                                                                           as srvcc_hosr_lte_gsm_succ_div_succ_plus_fail_denum

                     , nds(sum(num_erab_setup_success_rate_xhj), sum(denum_erab_setup_success_rate_xhj))                                                             as erab_setup_success_rate
                     , sum(num_erab_setup_success_rate_xhj)                                                                                                          as num_erab_setup_success_rate
                     , sum(denum_erab_setup_success_rate_xhj)                                                                                                        as denum_erab_setup_success_rate

                     , avg(data_integrity)                                                                                                                           as data_integrity

                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.region = 'PUMA'
--                   and n.site_id in ('AMB002')

                group by m.date
                       , r.branch
                       , r.rtpo
                       , r.kota_kabupaten)
select *
from master
order by kota_kabupaten, date
;