SELECT yearweek                                                                                                   AS week
     , 'PUMA'                                                                                                     as regional
     , SUM(payload_mbyte)                                                                                         AS Total_Payload
     , SUM(dl_traffic_volume_mbyte)                                                                               AS Downlink_Traffic_Volume2
     , SUM(ul_traffic_volume_mbyte)                                                                               AS Uplink_Traffic_Volume2
     , SUM(num_rrc_setup_sr)                                                                                      AS rrc_setup_success_rate_service_num
     , SUM(denum_rrc_setup_sr)                                                                                    AS rrc_setup_success_rate_service_den
     , ndp(sum(num_rrc_setup_sr), sum(denum_rrc_setup_sr))                                                        AS rrc_setup_success_rate_service
     , SUM(num_e_rab_setup_sr)                                                                                    AS E_Rab_Setup_SR_Numnew
     , SUM(denum_e_rab_setup_sr)                                                                                  AS E_Rab_Setup_SR_Denumnew
     , ndp(sum(num_e_rab_setup_sr), sum(denum_e_rab_setup_sr))                                                    AS E_Rab_Setup_SR

     , sum(num_rrc_setup_sr) * sum(num_e_rab_setup_sr) * sum(s1_signal_connection_est_sr_num)                     as cssr_ver_1_num
     , sum(denum_rrc_setup_sr) * sum(denum_e_rab_setup_sr) * sum(s1_signal_connection_est_sr_denum)               as cssr_ver_1_denum
     , ndp(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 sdr_ver_1_num
     , SUM(denum_e_rab_drop_rate_percent)                                                                         AS sdr_ver_1_denum
     , ndp(sum(num_e_rab_drop_rate_percent), sum(num_e_rab_drop_rate_percent))                                    AS SDR

     , SUM(num_ifho_sr)                                                                                           AS Intra_Frequency_Handover_SR_NUM
     , SUM(denum_ifho_sr)                                                                                         AS Intra_Frequency_Handover_SR_DENUM
     , ndp(sum(num_ifho_sr), sum(denum_ifho_sr))                                                                  AS Intra_Frequency_Handover_SR

     , SUM(cell_availability_num)                                                                                 AS Radio_Network_Availability_Rate_NUM
     , SUM(cell_availability_denum)                                                                               AS Radio_Network_Availability_Rate_DENUM
     , CASE
           WHEN SUM(cell_availability_denum) = 0 THEN 0
           ELSE SUM(cell_availability_num) / SUM(cell_availability_denum) * 100
    END                                                                                                           AS Radio_Network_Availability_Rate

     , 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_v1
     , AVG(user_ul_throughput_kbps)                                                                               AS user_ul_throughput_kbps
     , 0                                                                                                          AS z_avg_ulrssi_pucch_num
     , 0                                                                                                          AS z_ulrssi_pucch_denum
     , AVG(average_ni_of_carrier_dbm)                                                                             AS average_cell_rssi_dbm
     , SUM(maximum_number_of_rrc_connection_user_unit)                                                            AS Maximum_Active_User
     , SUM(num_csfb_sr)                                                                                           AS SWAP_4G_CSFB_NUM
     , SUM(denum_csfb_sr)                                                                                         AS SWAP_4G_CSFB_DENUM
     , CASE
           WHEN SUM(denum_csfb_sr) = 0 THEN 0
           ELSE SUM(num_csfb_sr) / SUM(denum_csfb_sr) * 100
    END                                                                                                           AS CSFB


     , SUM(successful_outgoing_handovers_execution_over_x2_times)                                                 AS successful_inter_enb_ho
     , SUM(attempted_outgoing_handovers_execution_over_x2_times)                                                  AS attempted_inter_enb_ho
     , CASE
           WHEN SUM(attempted_outgoing_handovers_execution_over_x2_times) = 0 THEN 0
           ELSE SUM(successful_outgoing_handovers_execution_over_x2_times) / SUM(attempted_outgoing_handovers_execution_over_x2_times) *
                100 END                                                                                           AS inter_enb_ho


     , SUM(successful_outgoing_handovers_execution_over_s1_times)                                                 AS successful_inter_enb_s1_ho
     , SUM(attempted_outgoing_handovers_execution_over_s1_times)                                                  AS attempted_inter_enb_s1_ho
     , CASE
           WHEN SUM(attempted_outgoing_handovers_execution_over_s1_times) = 0 THEN 0
           ELSE SUM(successful_outgoing_handovers_execution_over_s1_times) / SUM(attempted_outgoing_handovers_execution_over_s1_times) *
                100 END                                                                                           AS nter_enb_s1_ho
     , SUM(num_dl_prb_utilization_percent)                                                                        AS z_prbdl_num
     , SUM(denum_dl_prb_utilization_percent)                                                                      AS z_prbdl_denum
     , CASE
           WHEN SUM(denum_dl_prb_utilization_percent) = 0 THEN 0
           ELSE SUM(num_dl_prb_utilization_percent) / SUM(denum_dl_prb_utilization_percent) * 100 END             AS prb_dl_util

     , SUM(num_ul_prb_utilization_percent)                                                                        AS z_prbul_num
     , SUM(denum_ul_prb_utilization_percent)                                                                      AS z_prbul_denum
     , CASE
           WHEN SUM(denum_ul_prb_utilization_percent) = 0 THEN 0
           ELSE SUM(num_ul_prb_utilization_percent) / SUM(denum_ul_prb_utilization_percent) * 100 END             AS prb_ul_util

     , SUM(the_ratio_of_cqi_ge_7_monitor_num)                                                                     AS z_CQI7SR_Num
     , SUM(the_ratio_of_cqi_ge_7_monitor_denum)                                                                   AS z_CQI7SR_denum
     , CASE
           WHEN SUM(the_ratio_of_cqi_ge_7_monitor_denum) = 0 THEN 0
           ELSE SUM(the_ratio_of_cqi_ge_7_monitor_num) / SUM(the_ratio_of_cqi_ge_7_monitor_denum) * 100 END       AS z_CQI7SR

     , SUM(dl_64qam_modulation_scheme_usage_num)                                                                  AS z_64qamdl_num
     , SUM(dl_64qam_modulation_scheme_usage_denum)                                                                AS z_64qamdl_denum
     , CASE
           WHEN SUM(dl_64qam_modulation_scheme_usage_denum) = 0 THEN 0
           ELSE SUM(dl_64qam_modulation_scheme_usage_num) / SUM(dl_64qam_modulation_scheme_usage_denum) * 100 END AS z_64qam_dl_usage

     , SUM(ul_16qam_modulation_scheme_usage_num)                                                                  AS z_16qamul_num
     , SUM(ul_16qam_modulation_scheme_usage_denum)                                                                AS z_16qamul_denum
     , CASE
           WHEN SUM(ul_16qam_modulation_scheme_usage_denum) = 0 THEN 0
           ELSE SUM(ul_16qam_modulation_scheme_usage_num) / SUM(ul_16qam_modulation_scheme_usage_denum) * 100 END AS z_16qam_ul_usage

     , SUM(s1_signal_connection_est_sr_num)                                                                       AS z_NumS1
     , SUM(s1_signal_connection_est_sr_denum)                                                                     AS z_DenumS1
     , CASE
           WHEN SUM(s1_signal_connection_est_sr_denum) = 0 THEN 0
           ELSE SUM(s1_signal_connection_est_sr_num) / SUM(s1_signal_connection_est_sr_denum) * 100 END           AS S1_Signalling_SR

     , SUM(num_csfb_sr)                                                                                           as z_csfb_prep_num
     , SUM(denum_csfb_sr)                                                                                         as z_csfb_prep_denum
     , CASE
           WHEN SUM(denum_csfb_sr) = 0 THEN 0
           ELSE SUM(num_csfb_sr) / SUM(denum_csfb_sr) *
                100 END                                                                                           AS z_csfb_prep


     , SUM(number_of_redirection_requests_from_lte_to_utran_csfb_times)                                           AS number_of_redirection_requests_from_lte_to_utran_csfb

FROM r11.meas_4g_daily m
where date between date_from('202332') and date_to('202349')
group by yearweek
order by yearweek
;

with ref_sites (site_id) as (values (array ['AMB002'])),
     ref_date as (select array_agg(date::date) as date
                  from w
                  where date between '2023-12-01' and '2023-12-07');

