with master as (select m.date
                     , r.region
                     , r.branch
                     , r.cluster
                     , r.kota_kabupaten
                     , n.site_id
                     , n.ne_id
                     , n.sector
                     , n.cell_name
                     , n.cell

                     , sum(reserved_counter_183_times + reserved_counter_184_times + reserved_counter_185_times +
                           reserved_counter_186_times + reserved_counter_187_times + reserved_counter_188_times +
                           reserved_counter_189_times)                                                           as km_0_0546
                     , sum(reserved_counter_190_times + reserved_counter_191_times + reserved_counter_192_times +
                           reserved_counter_193_times + reserved_counter_194_times + reserved_counter_195_times) as km_0546_1015
                     , sum(reserved_counter_196_times + reserved_counter_197_times + reserved_counter_198_times +
                           reserved_counter_199_times + reserved_counter_200_times + reserved_counter_201_times +
                           reserved_counter_202_times)                                                           as km_1015_1562
                     , sum(reserved_counter_203_times + reserved_counter_204_times + reserved_counter_205_times +
                           reserved_counter_206_times + reserved_counter_207_times + reserved_counter_208_times +
                           reserved_counter_209_times)                                                           as km_1562_2109
                     , sum(reserved_counter_210_times + reserved_counter_211_times + reserved_counter_212_times +
                           reserved_counter_213_times + reserved_counter_214_times + reserved_counter_215_times +
                           reserved_counter_216_times)                                                           as km_2109_2656
                     , sum(reserved_counter_217_times + reserved_counter_218_times + reserved_counter_219_times +
                           reserved_counter_220_times + reserved_counter_221_times + reserved_counter_222_times) as km_2656_3125
                     , sum(reserved_counter_223_times + reserved_counter_224_times + reserved_counter_225_times +
                           reserved_counter_226_times + reserved_counter_227_times + reserved_counter_228_times +
                           reserved_counter_229_times)                                                           as km_3125_3906
                     , sum(reserved_counter_230_times + reserved_counter_231_times + reserved_counter_232_times +
                           reserved_counter_233_times)                                                           as km_3906_6328
                     , sum(reserved_counter_234_times + reserved_counter_235_times + reserved_counter_236_times +
                           reserved_counter_237_times + reserved_counter_238_times + reserved_counter_239_times) as km_6328_10078
                     , sum(reserved_counter_240_times + reserved_counter_241_times + reserved_counter_242_times +
                           reserved_counter_243_times + reserved_counter_244_times)                              as km_gt_10078
                     , sum(reserved_counter_183_times + reserved_counter_184_times + reserved_counter_185_times +
                           reserved_counter_186_times + reserved_counter_187_times + reserved_counter_188_times +
                           reserved_counter_189_times + reserved_counter_190_times + reserved_counter_191_times +
                           reserved_counter_192_times + reserved_counter_193_times + reserved_counter_194_times +
                           reserved_counter_195_times + reserved_counter_196_times + reserved_counter_197_times +
                           reserved_counter_198_times + reserved_counter_199_times + reserved_counter_200_times +
                           reserved_counter_201_times + reserved_counter_202_times + reserved_counter_203_times +
                           reserved_counter_204_times + reserved_counter_205_times + reserved_counter_206_times +
                           reserved_counter_207_times + reserved_counter_208_times + reserved_counter_209_times +
                           reserved_counter_210_times + reserved_counter_211_times + reserved_counter_212_times +
                           reserved_counter_213_times + reserved_counter_214_times + reserved_counter_215_times +
                           reserved_counter_216_times + reserved_counter_217_times + reserved_counter_218_times +
                           reserved_counter_219_times + reserved_counter_220_times + reserved_counter_221_times +
                           reserved_counter_222_times + reserved_counter_223_times + reserved_counter_224_times +
                           reserved_counter_225_times + reserved_counter_226_times + reserved_counter_227_times +
                           reserved_counter_228_times + reserved_counter_229_times + reserved_counter_230_times +
                           reserved_counter_231_times + reserved_counter_232_times + reserved_counter_233_times +
                           reserved_counter_234_times + reserved_counter_235_times + reserved_counter_236_times +
                           reserved_counter_237_times + reserved_counter_238_times + reserved_counter_239_times +
                           reserved_counter_240_times + reserved_counter_241_times + reserved_counter_242_times +
                           reserved_counter_243_times + reserved_counter_244_times)                              as km_total

                     , concat(n.ne_id, '-', n.sector)                                                            as ne_sector
                     , n.band

                from meas_4g_daily m
                         join network_cell n on m.network_cell_id = n.id
                         join ref_site r on r.site_id = n.site_id
                where date between '2023-12-01' and '2023-12-07'
                  and n.site_id in ('AMB002', 'AMT001', 'ADL001')
                group by m.date
                       , r.region
                       , r.branch
                       , r.cluster
                       , r.kota_kabupaten
                       , n.site_id
                       , n.ne_id
                       , n.sector
                       , n.cell_name
                       , n.cell
                       , concat(n.ne_id, '-', n.sector)
                       , n.band),
     agg as (select date
                  , region
                  , branch
                  , cluster
                  , kota_kabupaten
                  , site_id
                  , ne_id
                  , sector
                  , cell_name
                  , cell
                  , km_0_0546
                  , km_0546_1015
                  , km_1015_1562
                  , km_1562_2109
                  , km_2109_2656
                  , km_2656_3125
                  , km_3125_3906
                  , km_3906_6328
                  , km_6328_10078
                  , km_gt_10078
                  , km_total
                  , ne_sector
                  , band
                  , 100 * (km_0_0546) / nullif(km_total,0)                                                                                                                                        as km_0_0546_percentage
                  , 100 * (km_0_0546 + km_0546_1015) / nullif(km_total,0)                                                                                                                         as km_0546_1015_percentage
                  , 100 * (km_0_0546 + km_0546_1015 + km_1015_1562) / nullif(km_total,0)                                                                                                          as km_1015_1562_percentage
                  , 100 * (km_0_0546 + km_0546_1015 + km_1015_1562 + km_1562_2109) / nullif(km_total,0)                                                                                           as km_1562_2109_percentage
                  , 100 * (km_0_0546 + km_0546_1015 + km_1015_1562 + km_1562_2109 + km_2109_2656) / nullif(km_total,0)                                                                            as km_2109_2656_percentage
                  , 100 * (km_0_0546 + km_0546_1015 + km_1015_1562 + km_1562_2109 + km_2109_2656 + km_2656_3125) / nullif(km_total,0)                                                             as km_2656_3125_percentage
                  , 100 * (km_0_0546 + km_0546_1015 + km_1015_1562 + km_1562_2109 + km_2109_2656 + km_2656_3125 + km_3125_3906) / nullif(km_total,0)                                              as km_3125_3906_percentage
                  , 100 * (km_0_0546 + km_0546_1015 + km_1015_1562 + km_1562_2109 + km_2109_2656 + km_2656_3125 + km_3125_3906 + km_3906_6328) /
                    nullif(km_total,0)                                                                                                                                                            as km_3906_6328_percentage
                  , 100 * (km_0_0546 + km_0546_1015 + km_1015_1562 + km_1562_2109 + km_2109_2656 + km_2656_3125 + km_3125_3906 + km_3906_6328 + km_6328_10078) /
                    nullif(km_total,0)                                                                                                                                                            as km_6328_10078_percentage
                  , 100 * (km_0_0546 + km_0546_1015 + km_1015_1562 + km_1562_2109 + km_2109_2656 + km_2656_3125 + km_3125_3906 + km_3906_6328 + km_6328_10078 + km_gt_10078) /
                    nullif(km_total,0)                                                                                                                                                            as km_gt_10078_percentage
             from master)
select *
from agg
order by region, branch, cluster, kota_kabupaten, cell_name, date
;


-- C119 - C180
-- 19-25 0-0.546km
-- 26-31 0.546-1.015km
-- 32-38 1.015-1.562km
-- 39-45 1.562-2.109km
-- 46-52 2.109-2.656km
-- 53-58 2.656-3.125km
-- 59-65 3.125-3.906km
-- 66-69 3.906-6.328km
-- 70-75 6.328-10.078km
-- 76-80 >10km