with daily as (select date
                    , band
                    , site_id
                    , cell_name
                    , ne_id
                    , cell

                    , nds(sum(cell_availability_num), sum(cell_availability_denum))                                                                               as cell_availability
                    , nds(sum(num_rrc_setup_sr), sum(denum_rrc_setup_sr))                                                                                         as rrc_setup_sr
                    , nds(sum(num_e_rab_setup_sr), sum(denum_e_rab_setup_sr))                                                                                     as e_rab_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
                    , nds(sum(num_e_rab_drop_rate_percent), sum(denum_e_rab_drop_rate_percent))                                                                   as e_rab_drop_rate
                    , nds(sum(num_ifho_sr), sum(denum_ifho_sr))                                                                                                   as ifho_sr
                    , nds(sum(num_csfb_sr), sum(denum_csfb_sr))                                                                                                   as csfb_sr
                    , sum(payload_mbyte)                                                                                                                          as payload_mbyte
                    , nds(sum(inter_freq_lte_ho_num), sum(inter_freq_lte_ho_denum))                                                                               as inter_freq_lte_ho
                    , nds(sum(num_service_drop_rate), sum(denum_service_drop_rate))                                                                               as service_drop_rate
                    , 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(prb_number_used_on_pusch_channel_unit), sum(prb_number_available_on_pusch_channel_unit))                                            as pusch_prb_utilization_rate
                    , sum(maximum_number_of_rrc_connection_user_unit)                                                                                             as maximum_number_of_rrc_connection_user
                    , 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
                    , nds(sum(volte_call_drop_rate_num), sum(volte_call_drop_rate_denum))                                                                         as call_drop_rate
                    , nds(sum(volte_call_drop_rate_mme_num), sum(volte_call_drop_rate_mme_denum))                                                                 as call_drop_rate_mme
                    , nds(sum(volte_user_outgoing_intra_freq_hosr_num), sum(volte_user_outgoing_intra_freq_hosr_denum))                                           as user_outgoing_intra_freq_hosr
                    , 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_traffic_erl)                                                                                                                      as traffic_erl
                    , sum(dl_traffic_volume_mbyte + ul_traffic_volume_mbyte)                                                                                      as traffic_volume_mbyte


               from r11.meas_4g_daily m
                        join network_cell n
                             on m.network_cell_id = n.id
--                where date between date_int(7) and date_int(1)
               where date between '2023-12-01' and '2023-12-07'
               group by date
                      , band
                      , site_id
                      , cell_name
                      , ne_id
                      , cell),
     daily_bh as (select date
                       , band
                       , site_id
                       , cell_name
                       , ne_id
                       , cell
                       , avg(average_cqi)         as average_cqi
                       , avg(spectral_efficiency) as spectral_efficiency

                  from r11.meas_4g_daily_bh_activeuser m
                           join network_cell n
                                on m.network_cell_id = n.id
--                where date between date_int(7) and date_int(1)
                  where date between '2023-12-01' and '2023-12-07'
                  group by date
                         , band
                         , site_id
                         , cell_name
                         , ne_id
                         , cell),
     combined as (select d.date
                       , d.band
                       , d.site_id
                       , d.cell_name
                       , d.ne_id
                       , d.cell
                       , cell_availability
                       , rrc_setup_sr
                       , e_rab_setup_sr
                       , cssr
                       , e_rab_drop_rate
                       , ifho_sr
                       , csfb_sr
                       , dbh.spectral_efficiency
                       , dbh.average_cqi
                       , payload_mbyte
                       , inter_freq_lte_ho
                       , service_drop_rate
                       , dl_prb_utilization
                       , ul_prb_utilization
                       , pusch_prb_utilization_rate
                       , maximum_number_of_rrc_connection_user
                       , call_setup_sr_qci_1_qci_5
                       , call_drop_rate
                       , call_drop_rate_mme
                       , user_outgoing_intra_freq_hosr
                       , srvcc_hosr_lte_gsm_succ_div_succ_plus_fail
                       , traffic_erl
                       , traffic_volume_mbyte
                  from daily d
                           join daily_bh dbh
                                on d.date = dbh.date
                                    and d.site_id = dbh.site_id
                                    and d.ne_id = dbh.ne_id
                                    and d.cell_name = dbh.cell_name
                                    and d.cell = dbh.cell)
select *
from combined
order by cell_name, date;

