with master as (select m.date
                     , n.site_id
                     , sector
                     , band

                     , ndp(sum(num_dl_prb_utilization_percent), sum(denum_dl_prb_utilization_percent)) as dl_prb_utilization
                     , ndp(sum(num_ul_prb_utilization_percent), sum(denum_ul_prb_utilization_percent)) as ul_prb_utilization

                from r11.meas_4g_daily_bh_activeuser m
                         join network_cell n on m.network_cell_id = n.id

                where m.date between '2023-12-01' and '2023-12-07'
                  and n.site_id in
                      ('AMB012', 'AMB025', 'AMB053', 'AMB065', 'AMB095', 'AMB109', 'AMB113', 'AMB117', 'AMB124', 'AMB128', 'AMB142', 'AMB191', 'AMB194', 'AMB275', 'AMB403', 'COK224', 'COK284', 'COK288',
                       'MSH016', 'MSH020', 'MSH070', 'MSH072', 'MSH098', 'MSH100', 'MSH106', 'MSH114', 'MSH119', 'MSH157', 'MSH158', 'MSH265', 'MSH266', 'MSH303', 'MSH304', 'MSH366', 'MSH387', 'MSH411',
                       'MSH414', 'MSH422', 'MSH424', 'MSH433', 'MSH607', 'MSH609', 'MSH626', 'MSH627', 'MSH628', 'MSH630', 'NLA009', 'NLA022', 'NLA044', 'NLA059', 'NLA091', 'NLA148', 'NLA152', 'NLA154',
                       'NLA231', 'NLA383', 'NTK075', 'SML060', 'SML090', 'TUL027', 'TUL028', 'TUL031', 'TUL055', 'TUL059', 'TUL066', 'TUL208', 'TUL142', 'MSH010', 'MSH255', 'COK179', 'JAP054', 'JAP056',
                       'JAP067', 'JAP125', 'JAP157', 'JAP160', 'JAP169', 'JAP171', 'JAP178', 'JAP182', 'JAP186', 'JAP198', 'JAP258', 'JAP345', 'JAP354', 'JAP362', 'JAP391', 'JAP413', 'JAP432', 'JAP454',
                       'JAP459', 'JAP472', 'JAP482', 'JAP501', 'JAP516', 'JAP823', 'JAP827', 'JAP830', 'SMI004', 'SMI016', 'WRS013', 'WRS033', 'WRS053', 'WRS086', 'WRS108', 'WRS111', 'JAP380', 'BIA058',
                       'BIA061', 'BIA082', 'BIA198', 'BIA212', 'BIA275', 'BIA280', 'BIA281', 'BIA282', 'BTI044', 'BTI103', 'BTI904', 'BTI906', 'COK278', 'COK502', 'MWR002', 'MWR006', 'MWR006', 'MWR006',
                       'MWR009', 'MWR013', 'MWR046', 'MWR072', 'MWR077', 'MWR106', 'MWR110', 'MWR119', 'MWR127', 'MWR182', 'MWR183', 'MWR185', 'MWR217', 'MWR218', 'MWR222', 'MWR303', 'MWR308', 'MWR366',
                       'MWR699', 'NAB003', 'NAB014', 'NAB017', 'NAB030', 'NAB036', 'NAB045', 'NAB056', 'NAB067', 'NAB070', 'NAB076', 'NAB093', 'NAB203', 'NAB205', 'NAB208', 'NAB211', 'NTK042', 'NTK059',
                       'NTK060', 'NTK061', 'SRU002', 'SRU005', 'SRU108', 'SRU109', 'BTI907', 'NAB207', 'MWR319', 'MWR041', 'COK305', 'MXX133', 'SON006', 'SON037', 'SON040', 'SON057', 'SON092', 'SON221',
                       'SON226', 'SON233', 'SON246', 'SON261', 'SON265', 'SON298', 'SON327', 'SON329', 'SON523', 'TMB002', 'TMB009', 'TMB061', 'WSI002', 'WSI014', 'WSI100', 'COK254', 'MRK101', 'MRK125',
                       'MRK228', 'MRK235', 'MRK263', 'TIM004', 'TIM010', 'TIM027', 'TIM056', 'TIM057', 'TIM059', 'TIM064', 'TIM069', 'TIM074', 'TIM075', 'TIM083', 'TIM093', 'TIM095', 'TIM121', 'TIM131',
                       'TIM132', 'TIM142', 'TIM145', 'TIM148', 'TIM149', 'TIM177', 'TIM191', 'TIM194', 'TIM202', 'TIM215', 'TIM223', 'TIM462')

                group by m.date
                       , n.site_id
                       , sector
                       , band
                order by site_id, sector, date)
select date
     , site_id
     , sector
     , avg(case when band = 'LTE900' then dl_prb_utilization end)     as LTE900
     , avg(case when band = 'LTE1800' then dl_prb_utilization end)    as LTE1800
     , avg(case when band = 'LTE2100' then dl_prb_utilization end)    as LTE2100
     , avg(case when band = 'LTE2300 F1' then dl_prb_utilization end) as LTE2300_F1
     , avg(case when band = 'LTE2300 F2' then dl_prb_utilization end) as LTE2300_F2
     , avg(case when band = 'LTE2300 F3' then dl_prb_utilization end) as LTE2300_F3
     , max(dl_prb_utilization)                                        as max_prb
     , min(dl_prb_utilization)                                        as min_prb
     , max(dl_prb_utilization) - min(dl_prb_utilization)              as delta_prb
     , case
           when max(dl_prb_utilization) - min(dl_prb_utilization) > 50 then '>50%'
           when max(dl_prb_utilization) - min(dl_prb_utilization) < 30 then 'balance'
           else '30-50%' end                                          as remark
from master
group by date, site_id, sector
order by site_id, sector, date
;