with raw as (select datetime
                  , date
                  , n.site_id
                  , sector

                  , 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

                  , avg(data_integrity)                                                                                                         as data_integrity
                  , 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(data_integrity)                                                                                                         as data_integrity_

             from r11.meas_4g_hourly raw
                      join network_cell n on raw.network_cell_id = n.id
                      join ref_site ref on n.site_id = ref.site_id

             where date = '2023-12-05'
--              where date between '2023-10-20' and '2023-11-09'

--     where raw.date >= date(current_date - '7d'::interval)
--       and raw.site_id in
--           ('AGA041','AMB014','AMB025','AMB126','BIA174','BTI008','BTI008','BTI008','BTI044','BTI044','BTI044','BTI102','BTI214','BTI905','BTI905','BTI906','BTI906','BTI906','BTI907','BTI907','COK071','COK072','COK124','COK214','COK214','COK264','COK336','COK502','DBO001','ENT002','ENT002','ENT002','ENT002','ENT006','ENT008','ENT166','ENT166','ENT314','JAP056','JAP056','JAP125','JAP747','JAP747','JAP828','JAP828','MRK006','MRK093','MRK814','MRK820','MSH004','MSH011','MSH011','MSH013','MSH070','MSH118','MSH119','MSH133','MSH133','MSH205','MSH207','MSH238','MSH369','MSH374','MSH374','MSH378','MSH383','MSH385','MSH392','MSH408','MSH424','MSH424','MSH443','MSH629','MSH629','MSH629','MSH630','MUL001','MUL001','MUL001','MUL053','MUL053','MWR007','MWR032','MWR062','MWR147','MWR182','MWR369','MXW003','MXW003','MXW060','MXW073','MXW090','MXX122','MXX133','NAB011','NAB025','NAB027','NAB027','NAB029','NAB029','NAB046','NAB143','NAB207','NAB207','NAB207','NAB208','NAB208','NAB208','NAB210','NAB210','NAB210','NLA009','NLA013','NLA013','NLA013','NLA013','NLA033','NLA033','NLA033','NLA033','NLA033','NLA050','NLA211','NLA211','NLA382','NLA382','NLA383','NTK001','NTK002','NTK043','NTK063','NTK069','NTK074','NTK075','NTK080','SML011','SML012','SML014','SML086','SML111','SML111','SML112','SML150','SML254','SON179','SRU003','SRU007','SRU010','SRU015','SRU016','TIM026','TIM026','TIM027','TIM030','TIM085','TIM158','TIM167','TMR010','TMR010','TUL023','TUL035','TUL045','TUL146','TUL307','TUL307','UXX147','UYP141','WAM001','WAM012','WAM012','WAM012','WAM015','WAM023','WAM064','WRS009','WRS041','WRS111','WSI006','WSI071','WSI071','WSI071')

             group by datetime
                    , date
                    , server
                    , n.site_id
                    , sector

             having nds(sum(num_dl_prb_utilization_percent), sum(denum_dl_prb_utilization_percent)) > .9
)
select *
from raw
order by site_id, datetime;
