-- V1
with master_r08 as (select n.region
                         , n.tech
                         , m.yearweek
                         , sum(tch_traffic_erlang) as traffic_erl
                         , sum(payload_mbyte)      as payload_mbyte
                    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 m.date between date_from('202346') and date_to('202352')
                    group by n.region
                           , n.tech
                           , m.yearweek

                    union all

                    select n.region
                         , n.tech
                         , m.yearweek
                         , sum(volte_traffic_erl) as traffic
                         , sum(payload_mbyte)     as payload_mbyte
                    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 m.date between date_from('202346') and date_to('202352')
                    group by n.region
                           , n.tech
                           , m.yearweek

                    union all

                    select n.region
                         , n.tech
                         , m.yearweek
                         , 0            as traffic
                         , sum(payload) as payload_mbyte
                    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 m.date between date_from('202346') and date_to('202352')
                    group by n.region
                           , n.tech
                           , m.yearweek),
     master_r09 as (select n.region
                         , n.tech
                         , m.yearweek
                         , sum(tch_traffic_erlang) as traffic_erl
                         , sum(payload_mbyte)      as payload_mbyte
                    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 m.date between date_from('202346') and date_to('202352')
                    group by n.region
                           , n.tech
                           , m.yearweek

                    union all

                    select n.region
                         , n.tech
                         , m.yearweek
                         , sum(volte_traffic_erl) as traffic
                         , sum(payload_mbyte)     as payload_mbyte
                    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 m.date between date_from('202346') and date_to('202352')
                    group by n.region
                           , n.tech
                           , m.yearweek

                    union all

                    select n.region
                         , n.tech
                         , m.yearweek
                         , 0            as traffic
                         , sum(payload) as payload_mbyte
                    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 m.date between date_from('202346') and date_to('202352')
                    group by n.region
                           , n.tech
                           , m.yearweek),
     master_r11 as (select n.region
                         , n.tech
                         , m.yearweek
                         , sum(tch_traffic_erlang) as traffic_erl
                         , sum(payload_mbyte)      as payload_mbyte
                    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 m.date between date_from('202346') and date_to('202352')
                    group by n.region
                           , n.tech
                           , m.yearweek

                    union all

                    select n.region
                         , n.tech
                         , m.yearweek
                         , sum(volte_traffic_erl) as traffic
                         , sum(payload_mbyte)     as payload_mbyte
                    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 m.date between date_from('202346') and date_to('202352')
                    group by n.region
                           , n.tech
                           , m.yearweek

                    union all

                    select n.region
                         , n.tech
                         , m.yearweek
                         , 0            as traffic
                         , sum(payload) as payload_mbyte
                    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 m.date between date_from('202346') and date_to('202352')
                    group by n.region
                           , n.tech
                           , m.yearweek),
     combined as (select *
                  from master_r08
                  union all
                  select *
                  from master_r09
                  union all
                  select *
                  from master_r11)
select region
     , tech
     , yearweek
     , traffic_erl::numeric(100, 2)   as traffic_erl
     , payload_mbyte::numeric(100, 2) as payload_mbyte
from combined
order by region, tech, yearweek
;


-- V2 lebih cepat
with master as (select region
                     , tech
                     , yearweek
                     , sum(tch_traffic_erlang) as traffic_erl
                     , sum(payload_mbyte)      as payload_mbyte
                from app.fact_category_kpi_2g_daily
                where level_type = 'REGION'
                  and all_band = true
                  and date between date_from('202346') and date_to('202352')
                group by region, tech, yearweek

                union all

                select region
                     , tech
                     , yearweek
                     , sum(volte_traffic_erl) as traffic
                     , sum(payload_mbyte)     as payload_mbyte
                from app.fact_category_kpi_4g_daily
                where level_type = 'REGION'
                  and all_band = true
                  and date between date_from('202346') and date_to('202352')
                group by region, tech, yearweek

                union all

                select region
                     , tech
                     , yearweek
                     , 0            as traffic
                     , sum(payload) as payload_mbyte
                from app.fact_category_kpi_5g_daily
                where level_type = 'REGION'
                  and all_band = true
                  and date between date_from('202346') and date_to('202352')
                group by region, tech, yearweek)
select region
     , tech
     , yearweek
     , traffic_erl::numeric(100, 2)   as traffic_erl
     , payload_mbyte::numeric(100, 2) as payload_mbyte
from master
order by region, tech, yearweek
;


