-- Productivity
with master_08 as (select n.tech
                        , yearweek
                        , date
                        , kota_kabupaten
                        , band

                        , sum(volte_traffic_erl)                                 as traffic_erl
                        , 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
                        , 0                                                      as avg_of_maximum_number_of_rrc_connection_user_unit
                        , max(maximum_number_of_rrc_connection_user_unit)        as maximum_number_of_rrc_connection_user_unit

                        , sum(sum_of_active_users_rnp)                           as sum_of_active_users_rnp
                        , avg(active_users_rnp)                                  as avg_of_active_users_rnp
                        , max(max_of_active_users_rnp)                           as max_of_active_users_rnp


                   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 date between '2023-12-01' and '2023-12-07'
                   group by n.tech
                          , yearweek
                          , date
                          , kota_kabupaten
                          , band

                   union all

                   select n.tech
                        , yearweek
                        , date
                        , kota_kabupaten
                        , band

                        , sum(tch_traffic_erlang) as traffic_erl
                        , sum(payload_mbyte)      as payload_mbyte

                        , 0                       as sum_of_maximum_number_of_rrc_connection_user_unit
                        , 0                       as avg_of_maximum_number_of_rrc_connection_user_unit
                        , 0                       as maximum_number_of_rrc_connection_user_unit

                        , 0                       as sum_of_active_users_rnp
                        , 0                       as avg_of_active_users_rnp
                        , 0                       as max_of_active_users_rnp


                   from r08.meas_2g_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 date between '2023-12-01' and '2023-12-07'
                   group by n.tech
                          , yearweek
                          , date
                          , kota_kabupaten
                          , band

                   union all

                   select n.tech
                        , yearweek
                        , date
                        , kota_kabupaten
                        , band

                        , 0            as traffic_erl
                        , sum(payload) as payload_mbyte

                        , 0            as sum_of_maximum_number_of_rrc_connection_user_unit
                        , 0            as avg_of_maximum_number_of_rrc_connection_user_unit
                        , 0            as maximum_number_of_rrc_connection_user_unit

                        , 0            as sum_of_active_users_rnp
                        , 0            as avg_of_active_users_rnp
                        , 0            as max_of_active_users_rnp


                   from r08.meas_5g_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 date between '2023-12-01' and '2023-12-07'
                   group by n.tech
                          , yearweek
                          , date
                          , kota_kabupaten
                          , band),
     master_09 as (select n.tech
                        , yearweek
                        , date
                        , kota_kabupaten
                        , band

                        , sum(volte_traffic_erl)                                 as traffic_erl
                        , 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
                        , 0                                                      as avg_of_maximum_number_of_rrc_connection_user_unit
                        , max(maximum_number_of_rrc_connection_user_unit)        as maximum_number_of_rrc_connection_user_unit

                        , sum(sum_of_active_users_rnp)                           as sum_of_active_users_rnp
                        , avg(active_users_rnp)                                  as avg_of_active_users_rnp
                        , max(max_of_active_users_rnp)                           as max_of_active_users_rnp

                   from r09.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 date between '2023-12-01' and '2023-12-07'
                   group by n.tech
                          , yearweek
                          , date
                          , kota_kabupaten
                          , band


                   union all

                   select n.tech
                        , yearweek
                        , date
                        , kota_kabupaten
                        , band

                        , sum(tch_traffic_erlang) as traffic_erl
                        , sum(payload_mbyte)      as payload_mbyte

                        , 0                       as sum_of_maximum_number_of_rrc_connection_user_unit
                        , 0                       as avg_of_maximum_number_of_rrc_connection_user_unit
                        , 0                       as maximum_number_of_rrc_connection_user_unit

                        , 0                       as sum_of_active_users_rnp
                        , 0                       as avg_of_active_users_rnp
                        , 0                       as max_of_active_users_rnp


                   from r09.meas_2g_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 date between '2023-12-01' and '2023-12-07'
                   group by n.tech
                          , yearweek
                          , date
                          , kota_kabupaten
                          , band

                   union all

                   select n.tech
                        , yearweek
                        , date
                        , kota_kabupaten
                        , band

                        , 0            as traffic_erl
                        , sum(payload) as payload_mbyte

                        , 0            as sum_of_maximum_number_of_rrc_connection_user_unit
                        , 0            as avg_of_maximum_number_of_rrc_connection_user_unit
                        , 0            as maximum_number_of_rrc_connection_user_unit

                        , 0            as sum_of_active_users_rnp
                        , 0            as avg_of_active_users_rnp
                        , 0            as max_of_active_users_rnp


                   from r09.meas_5g_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 date between '2023-12-01' and '2023-12-07'
                   group by n.tech
                          , yearweek
                          , date
                          , kota_kabupaten
                          , band),
     master_11 as (select n.tech
                        , yearweek
                        , date
                        , kota_kabupaten
                        , band

                        , sum(volte_traffic_erl)                                 as traffic_erl
                        , 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
                        , 0                                                      as avg_of_maximum_number_of_rrc_connection_user_unit
                        , max(maximum_number_of_rrc_connection_user_unit)        as maximum_number_of_rrc_connection_user_unit

                        , sum(sum_of_active_users_rnp)                           as sum_of_active_users_rnp
                        , avg(active_users_rnp)                                  as avg_of_active_users_rnp
                        , max(max_of_active_users_rnp)                           as max_of_active_users_rnp

                   from r11.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 date between '2023-12-01' and '2023-12-07'
                   group by n.tech
                          , yearweek
                          , date
                          , kota_kabupaten
                          , band

                   union all

                   select n.tech
                        , yearweek
                        , date
                        , kota_kabupaten
                        , band

                        , sum(tch_traffic_erlang) as traffic_erl
                        , sum(payload_mbyte)      as payload_mbyte

                        , 0                       as sum_of_maximum_number_of_rrc_connection_user_unit
                        , 0                       as avg_of_maximum_number_of_rrc_connection_user_unit
                        , 0                       as maximum_number_of_rrc_connection_user_unit

                        , 0                       as sum_of_active_users_rnp
                        , 0                       as avg_of_active_users_rnp
                        , 0                       as max_of_active_users_rnp


                   from r11.meas_2g_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 date between '2023-12-01' and '2023-12-07'
                   group by n.tech
                          , yearweek
                          , date
                          , kota_kabupaten
                          , band

                   union all

                   select n.tech
                        , yearweek
                        , date
                        , kota_kabupaten
                        , band

                        , 0            as traffic_erl
                        , sum(payload) as payload_mbyte

                        , 0            as sum_of_maximum_number_of_rrc_connection_user_unit
                        , 0            as avg_of_maximum_number_of_rrc_connection_user_unit
                        , 0            as maximum_number_of_rrc_connection_user_unit

                        , 0            as sum_of_active_users_rnp
                        , 0            as avg_of_active_users_rnp
                        , 0            as max_of_active_users_rnp


                   from r11.meas_5g_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 date between '2023-12-01' and '2023-12-07'
                   group by n.tech
                          , yearweek
                          , date
                          , kota_kabupaten
                          , band),
     combined as (select *
                  from master_08
                  union all
                  select *
                  from master_09
                  union all
                  select *
                  from master_11)

select tech
     , yearweek
     , date
     , kota_kabupaten
     , band
     , traffic_erl::numeric(100, 2)   as traffic_erl
     , payload_mbyte::numeric(100, 2) as payload_mbyte
     , sum_of_maximum_number_of_rrc_connection_user_unit
     , avg_of_maximum_number_of_rrc_connection_user_unit
     , maximum_number_of_rrc_connection_user_unit
     , sum_of_active_users_rnp
     , avg_of_active_users_rnp
     , max_of_active_users_rnp
from combined
order by kota_kabupaten, band, date
;

