-- Productivity
with master_11 as (select n.tech
                        , yearweek
                        , date
                        , n.site_id

                        , sum(volte_traffic_erl) as traffic_erl
                        , 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 date between date_from('202340') and date_to('202340')
                   group by n.tech
                          , yearweek
                          , date
                          , n.site_id

                   union all

                   select n.tech
                        , yearweek
                        , date
                        , n.site_id

                        , 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 date between date_from('202340') and date_to('202340')
                   group by n.tech
                          , yearweek
                          , date
                          , n.site_id

                   union all

                   select n.tech
                        , yearweek
                        , date
                        , n.site_id

                        , 0            as traffic_erl
                        , 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 date between date_from('202340') and date_to('202340')
                   group by n.tech
                          , yearweek
                          , date
                          , n.site_id)

select tech
     , yearweek
     , site_id
     , avg(traffic_erl)::numeric(100, 2)   as avg_week_traffic_erl
     , avg(payload_mbyte)::numeric(100, 2) as avg_week_payload_mbyte
from master_11
group by tech, yearweek, site_id
order by tech, site_id, yearweek
;

