with datarange as (select date
                   from w
                   where date between date_from(to_week(date_int(7)))
                             and date_to(to_week(date_int(7)))),
     master as (select '2G'                        as tech,
                       raw.date                    as datetime,
                       ref.branch,
                       ref.cluster                 as cluster,
                       sum(raw.tch_traffic_erlang) as traffic,
                       sum(raw.payload_mbyte)      as payload_mbyte
                from r11.meas_2g_daily raw
                         join network_cell net on raw.network_cell_id = net.id
                         join ref_site ref on net.site_id = ref.site_id
                         join datarange dt on raw.date = dt.date
                group by raw.date, ref.branch, ref.cluster
                union
                select '4G'                   as tech,
                       raw.date               as datetime,
                       ref.branch,
                       ref.cluster            as cluster,
                       0.0                    as traffic_voice,
                       sum(raw.payload_mbyte) as payload_mbyte
                from r11.meas_4g_daily raw
                         join network_cell net on raw.network_cell_id = net.id
                         join ref_site ref on net.site_id = ref.site_id
                         join datarange dt on raw.date = dt.date
                group by raw.date, ref.branch, ref.cluster
                union
                select '5G'         as tech,
                       raw.date     as datetime,
                       ref.branch,
                       ref.cluster  as cluster,
                       0            as traffic_voice,
                       sum(payload) as payload_mbyte
                from r11.meas_5g_daily raw
                         join network_cell net on raw.network_cell_id = net.id
                         join ref_site ref on net.site_id = ref.site_id
                         join datarange dt on raw.date = dt.date
                group by raw.date, ref.branch, ref.cluster
                union
                select 'VOLTE'                                                     as tech,
                       raw.date                                                    as datetime,
                       ref.branch,
                       ref.cluster                                                 as cluster,
                       sum(volte_traffic_erl)                                      as traffic_voice,
                       sum(dl_traffic_volume_mbyte) + sum(ul_traffic_volume_mbyte) as payload_mbyte
                from r11.meas_4g_daily raw
                         join network_cell net on raw.network_cell_id = net.id
                         join ref_site ref on net.site_id = ref.site_id
                         join datarange dt on raw.date = dt.date
                group by raw.date, ref.branch, ref.cluster),
     master2 as (select master.datetime,
                        master.branch,
                        master.cluster,
                        sum(
                                case
                                    when master.tech = '2G' then master.traffic
                                    else 0
                                    end) as traffic_2g,
                        sum(
                                case
                                    when master.tech = 'VOLTE' then master.traffic
                                    else 0
                                    end) as traffic_volte,
                        sum(
                                case
                                    when master.tech = '2G' then master.payload_mbyte
                                    else 0
                                    end) as payload_2g,
                        sum(
                                case
                                    when master.tech = '4G' then master.payload_mbyte
                                    else 0
                                    end) as payload_4g,
                        sum(
                                case
                                    when master.tech = '5G' then master.payload_mbyte
                                    else 0
                                    end) as payload_5g,
                        sum(
                                case
                                    when master.tech = 'VOLTE' then master.payload_mbyte
                                    else 0
                                    end) as payload_volte
                 from master
                 group by master.datetime, master.branch, master.cluster),
     final as (select to_week(raw.datetime)                                                                                                       as year_week,
                      right(to_week(raw.datetime), 2)::integer                                                                                    as week,
                      to_char(raw.datetime::timestamp with time zone, 'YYYY')                                                                     as year,
                      to_char(raw.datetime::timestamp with time zone, 'YYYY-MM')                                                                  as yearmonth,
                      to_char(raw.datetime::timestamp with time zone, 'MM')                                                                       as month,
                      to_char(raw.datetime::timestamp with time zone, 'DD-Mon')                                                                   as date,
                      to_char(raw.datetime::timestamp with time zone, 'DD')                                                                       as datefix,
                      raw.branch,
                      raw.cluster,
                      sum(raw.traffic_2g)::numeric(100, 2)                                                                                        as traffic_2g,
                      0                                                                                                                           as traffic_3g,
                      sum(raw.traffic_volte)::numeric(100, 2)                                                                                     as traffic_volte,
                      sum(raw.payload_2g)::numeric(100, 2)                                                                                        as payload_2g,
                      0                                                                                                                           as payload_3g,
                      sum(raw.payload_4g)::numeric(100, 2)                                                                                        as payload_4g,
                      sum(raw.payload_5g)::numeric(100, 2)                                                                                        as payload_5g,
                      sum(raw.payload_volte)::numeric(100, 2)                                                                                     as payload_volte,
                      (sum(raw.traffic_2g) + sum(raw.traffic_volte))::numeric(100, 2)                                                             as total_traffic_erl,
                      (sum(raw.payload_2g) + sum(raw.payload_4g) + sum(raw.payload_5g) + sum(raw.payload_volte))::numeric(100, 2)                 as total_payload_mb,
                      ((sum(raw.traffic_2g) + sum(raw.traffic_volte)) / 1000000)::numeric(100, 2)                                                 as traffic_mio,
                      ((sum(raw.payload_2g) + sum(raw.payload_4g) + sum(raw.payload_5g) + sum(raw.payload_volte)) / 1024 / 1024)::numeric(100, 2) as payload_tb
               from master2 raw
               group by raw.datetime, raw.branch, raw.cluster
               order by raw.datetime, raw.branch, raw.cluster)
select *
from final
;