with master as (select yearweek
                     , branch
                     , kota_kabupaten
                     , n.site_id
                     , n.cell
                     , n.cell_name
                     , n.band

                     , 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

                     , sum(payload_mbyte)                                                              as payload_mbyte
                     , sum(sum_of_maximum_number_of_rrc_connection_user_unit)                          as sum_of_maximum_number_of_rrc_connection_user_unit

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

                     , sum(se_num_dl)                                                                  as se_num_dl
                     , sum(se_denum_dl)                                                                as se_denum_dl
                     , sum(se_num_ul)                                                                  as se_num_ul
                     , sum(se_denum_ul)                                                                as se_denum_ul

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

                     , sum(num_dl_average_mcs)                                                         as num_dl_average_mcs
                     , sum(denum_dl_average_mcs)                                                       as denum_dl_average_mcs
                     , avg(dl_average_mcs)                                                             as dl_average_mcs

                     , sum(num_ul_average_mcs)                                                         as num_ul_average_mcs
                     , sum(denum_ul_average_mcs)                                                       as denum_ul_average_mcs
                     , avg(ul_average_mcs)                                                             as ul_average_mcs

                from r08.meas_4g_daily_bdbh_activeuser m
                         join network_cell n on m.network_cell_id = n.id
                         join ref_site r on r.site_id = n.site_id
                where date between date_from('202350') and date_to('202350')
                group by yearweek
                       , branch
                       , kota_kabupaten
                       , n.site_id
                       , n.cell
                       , n.cell_name
                       , n.band

                union all

                select yearweek
                     , branch
                     , kota_kabupaten
                     , n.site_id
                     , n.cell
                     , n.cell_name
                     , n.band

                     , 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

                     , sum(payload_mbyte)                                                              as payload_mbyte
                     , sum(sum_of_maximum_number_of_rrc_connection_user_unit)                          as sum_of_maximum_number_of_rrc_connection_user_unit

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

                     , sum(se_num_dl)                                                                  as se_num_dl
                     , sum(se_denum_dl)                                                                as se_denum_dl
                     , sum(se_num_ul)                                                                  as se_num_ul
                     , sum(se_denum_ul)                                                                as se_denum_ul

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

                     , sum(num_dl_average_mcs)                                                         as num_dl_average_mcs
                     , sum(denum_dl_average_mcs)                                                       as denum_dl_average_mcs
                     , avg(dl_average_mcs)                                                             as dl_average_mcs

                     , sum(num_ul_average_mcs)                                                         as num_ul_average_mcs
                     , sum(denum_ul_average_mcs)                                                       as denum_ul_average_mcs
                     , avg(ul_average_mcs)                                                             as ul_average_mcs

                from r09.meas_4g_daily_bdbh_activeuser m
                         join network_cell n on m.network_cell_id = n.id
                         join ref_site r on r.site_id = n.site_id
                where date between date_from('202350') and date_to('202350')
                group by yearweek
                       , branch
                       , kota_kabupaten
                       , n.site_id
                       , n.cell
                       , n.cell_name
                       , n.band

                union all

                select yearweek
                     , branch
                     , kota_kabupaten
                     , n.site_id
                     , n.cell
                     , n.cell_name
                     , n.band

                     , 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

                     , sum(payload_mbyte)                                                              as payload_mbyte
                     , sum(sum_of_maximum_number_of_rrc_connection_user_unit)                          as sum_of_maximum_number_of_rrc_connection_user_unit

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

                     , sum(se_num_dl)                                                                  as se_num_dl
                     , sum(se_denum_dl)                                                                as se_denum_dl
                     , sum(se_num_ul)                                                                  as se_num_ul
                     , sum(se_denum_ul)                                                                as se_denum_ul

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

                     , sum(num_dl_average_mcs)                                                         as num_dl_average_mcs
                     , sum(denum_dl_average_mcs)                                                       as denum_dl_average_mcs
                     , avg(dl_average_mcs)                                                             as dl_average_mcs

                     , sum(num_ul_average_mcs)                                                         as num_ul_average_mcs
                     , sum(denum_ul_average_mcs)                                                       as denum_ul_average_mcs
                     , avg(ul_average_mcs)                                                             as ul_average_mcs

                from r11.meas_4g_daily_bdbh_activeuser m
                         join network_cell n on m.network_cell_id = n.id
                         join ref_site r on r.site_id = n.site_id
                where date between date_from('202350') and date_to('202350')
                group by yearweek
                       , branch
                       , kota_kabupaten
                       , n.site_id
                       , n.cell
                       , n.cell_name
                       , n.band)
select *
from master
order by branch, kota_kabupaten, cell_name, yearweek
;