with ref_week (week) as (values ('202349')),
     master as (select datetime
                     , date
                     , yearweek
                     , n.site_id
                     , n.ne_id
                     , n.cell_name                                                                                                      as cellname
                     , n.cell                                                                                                           as ci
                     , ndp(num_dl_prb_utilization_percent, denum_dl_prb_utilization_percent)                                            as prbdl
                     , case when ndp(num_dl_prb_utilization_percent, denum_dl_prb_utilization_percent) >= 90 then 'High' else 'Low' end as flag
                from r11.meas_4g_hourly m
                         join network_cell n on m.network_cell_id = n.id
                where date between date_from((select week from ref_week limit 1))
                    and date_to((select week from ref_week limit 1))
                  and n.site_id in ('NTK067', 'NTK068')),
     master_counting as (select yearweek as week, date, site_id, cellname, ci, count(*) as count, avg(prbdl) as prbdl
                         from master
                         where flag = 'High'
                         group by yearweek, date, site_id, cellname, ci

                         union

                         select yearweek as week, date, site_id, cellname, ci, count(*) as count, avg(prbdl) as prbdl
                         from master
                         where flag = 'Low'
                         group by yearweek, date, site_id, cellname, ci),
     master_unique as (select step2.week
                            , dl.date
                            , step2.site_id
                            , step2.cellname
                            , step2.ci
                       from w dl,
                            (select week, site_id, cellname, ci
                             from master_counting
                             group by week, site_id, cellname, ci) step2
                       where dl.week = (select week from ref_week limit 1)),
     master_combine as (select mu.week
                             , mu.date
                             , mu.site_id
                             , mu.cellname
                             , mu.ci
                             , case when mc.count is null then 0 else mc.count end count
                             , mc.prbdl
                        from master_unique mu
                                 left join master_counting mc
                                           on mu.week = mc.week
                                               and mu.date = mc.date
                                               and mu.site_id = mc.site_id
                                               and mu.cellname = mc.cellname
                        order by mu.cellname, mu.date),
     master_agg as (select week, site_id, cellname, ci, string_agg(count::text, ',' order by date) as count, avg(prbdl) as avg_prbdl
                    from master_combine
                    group by week, site_id, cellname, ci),
     result as (select week

                     , site_id
                     , cellname
                     , ci

                     , avg_prbdl

                     , split_part(count, ',', 1)                                                    as date1_count
                     , split_part(count, ',', 2)                                                    as date2_count
                     , split_part(count, ',', 3)                                                    as date3_count
                     , split_part(count, ',', 4)                                                    as date4_count
                     , split_part(count, ',', 5)                                                    as date5_count
                     , split_part(count, ',', 6)                                                    as date6_count
                     , split_part(count, ',', 7)                                                    as date7_count
                     , case when split_part(count, ',', 1)::integer >= 8 then 'High' else 'Low' end as date1_status
                     , case when split_part(count, ',', 2)::integer >= 8 then 'High' else 'Low' end as date2_status
                     , case when split_part(count, ',', 3)::integer >= 8 then 'High' else 'Low' end as date3_status
                     , case when split_part(count, ',', 4)::integer >= 8 then 'High' else 'Low' end as date4_status
                     , case when split_part(count, ',', 5)::integer >= 8 then 'High' else 'Low' end as date5_status
                     , case when split_part(count, ',', 6)::integer >= 8 then 'High' else 'Low' end as date6_status
                     , case when split_part(count, ',', 7)::integer >= 8 then 'High' else 'Low' end as date7_status
                     , case when split_part(count, ',', 1)::integer >= 8 then 1 else 0 end +
                       case when split_part(count, ',', 2)::integer >= 8 then 1 else 0 end +
                       case when split_part(count, ',', 3)::integer >= 8 then 1 else 0 end +
                       case when split_part(count, ',', 4)::integer >= 8 then 1 else 0 end +
                       case when split_part(count, ',', 5)::integer >= 8 then 1 else 0 end +
                       case when split_part(count, ',', 6)::integer >= 8 then 1 else 0 end +
                       case when split_part(count, ',', 7)::integer >= 8 then 1 else 0 end          as total_count
                     , case
                           when (case when split_part(count, ',', 1)::integer >= 8 then 1 else 0 end +
                                 case when split_part(count, ',', 2)::integer >= 8 then 1 else 0 end +
                                 case when split_part(count, ',', 3)::integer >= 8 then 1 else 0 end +
                                 case when split_part(count, ',', 4)::integer >= 8 then 1 else 0 end +
                                 case when split_part(count, ',', 5)::integer >= 8 then 1 else 0 end +
                                 case when split_part(count, ',', 6)::integer >= 8 then 1 else 0 end +
                                 case when split_part(count, ',', 7)::integer >= 8 then 1 else 0 end) >= 3
                               then 'High Util'
                           else 'Low Util' end                                                      as final
                from master_agg
                order by cellname)
select *
from result
order by week, cellname     