with master as (select n.tech
                     , m.yearweek
                     , r.branch
                     , sum(tch_traffic_erlang) as traffic
                     , 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 date between date_from('202332') and date_to('202349')
                group by n.tech
                       , m.yearweek
                       , r.branch

                union

                select n.tech
                     , m.yearweek
                     , r.branch

                     , 0                    AS traffic
                     , sum(m.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 date between date_from('202332') and date_to('202349')
                group by n.tech
                       , m.yearweek
                       , r.branch),
     result as (select yearweek                                                                                              as week
                     , branch                                                                                                as zone
                     , sum(case when tech = '2G' then traffic end)                                                           as traffic_2g
                     , 0                                                                                                     as traffic_3g
                     , sum(case when tech = '2G' then payload_mbyte end)                                                     as payload_mbyte_2g
                     , 0                                                                                                     as payload_mbyte_3g
                     , sum(case when tech = '4G' then payload_mbyte end)                                                     as payload_mbyte_4g
                     , sum(case when tech = '2G' then traffic end)                                                           as traffic_total
                     , sum(case when tech = '2G' then payload_mbyte end) + sum(case when tech = '4G' then payload_mbyte end) as payload_mbyte_total
                from master
                group by yearweek
                       , branch
                order by yearweek
                       , branch)
select *
from result
order by week, zone;