with master as (select m.date
                     , r.kota_kabupaten

                     , sum(tch_traffic_erlang)                                       as tch_traffic_erlang
                     , sum(sdcch_traffic_erlang)                                     as sdcch_traffic_erlang
                     , sum(edge_payload_dl_mbyte)                                    as edge_payload_dl_mbyte
                     , sum(edge_payload_ul_mbyte)                                    as edge_payload_ul_mbyte
                     , sum(gprs_payload_mbyte)                                       as gprs_payload_mbyte
                     , sum(num_tch_availability)                                     as num_tch_availability
                     , sum(denum_tch_availability)                                   as denum_tch_availability
                     , ndp(sum(num_tch_availability), sum(denum_tch_availability))   as tch_availability
                     , sum(num_sdsr)                                                 as num_sdsr
                     , sum(denum_sdsr)                                               as denum_sdsr
                     , ndp(sum(num_sdsr), sum(denum_sdsr))                           as sdsr
                     , sum(num_hosr)                                                 as num_hosr
                     , sum(denum_hosr)                                               as denum_hosr
                     , ndp(sum(num_hosr), sum(denum_hosr))                           as hosr
                     , sum(num_tch_drop)                                             as num_tch_drop
                     , sum(denum_tch_drop)                                           as denum_tch_drop
                     , case
                           when ndp(sum(num_tch_drop), sum(denum_tch_drop)) < 0 then 0
                           else ndp(sum(num_tch_drop), sum(denum_tch_drop)) end      as tch_drop
                     , sum(num_tbf_dl_est_sr)                                        as num_tbf_dl_est_sr
                     , sum(denum_tbf_dl_est_sr)                                      as denum_tbf_dl_est_sr
                     , ndp(sum(num_tbf_dl_est_sr), sum(denum_tbf_dl_est_sr))         as tbf_dl_est_sr
                     , sum(num_tbf_ul_est_sr)                                        as num_tbf_ul_est_sr
                     , sum(denum_tbf_ul_est_sr)                                      as denum_tbf_ul_est_sr
                     , ndp(sum(num_tbf_ul_est_sr), sum(denum_tbf_ul_est_sr))         as tbf_ul_est_sr
                     , sum(num_tbf_comp_sr)                                          as num_tbf_comp_sr
                     , sum(denum_tbf_comp_sr)                                        as denum_tbf_comp_sr
                     , ndp(sum(num_tbf_comp_sr), sum(denum_tbf_comp_sr))             as tbf_comp_sr
                     , sum(num_tch_block)                                            as num_tch_block
                     , sum(denum_tch_blok)                                           as denum_tch_blok
                     , ndp(sum(num_tch_block), sum(denum_tch_blok))                  as tch_block
                     , sum(num_sdcch_block)                                          as num_sdcch_block
                     , sum(denum_sdcch_block)                                        as denum_sdcch_block
                     , ndp(sum(num_sdcch_block), sum(denum_sdcch_block))             as sdcch_block
                     , avg(edge_dl_throughput_kbps)                                  as edge_dl_throughput_kbps
                     , avg(gprs_dl_throughput_kbps)                                  as gprs_dl_throughput_kbps
                     , ndp(sum(radio_utilization_num), sum(radio_utilization_denum)) as radio_utilization
                     , sum(number_of_trxs)                                           as number_of_trxs
                     , sum(num_dl_qual_0_5)                                          as num_dl_qual_0_5
                     , sum(denum_dl_qual_0_5)                                        as denum_dl_qual_0_5
                     , ndp(sum(num_dl_qual_0_5), sum(denum_dl_qual_0_5))             as dl_qual_0_5
                     , sum(num_ul_qual_0_5)                                          as num_ul_qual_0_5
                     , sum(denum_ul_qual_0_5)                                        as denum_ul_qual_0_5
                     , ndp(sum(num_ul_qual_0_5), sum(denum_ul_qual_0_5))             as ul_qual_0_5
                     , sum(num_icm_interference)                                     as num_icm_interference
                     , sum(denum_icm_interference)                                   as denum_icm_interference
                     , sum(number_of_abis_delete_ind_messages)                       as number_of_abis_delete_ind_messages
                     , sum(number_of_abis_ps_paging_messages)                        as number_of_abis_ps_paging_messages
                     , sum(number_of_abis_interface_paging_command_messages)         as number_of_abis_interface_paging_command_messages
                     , sum(tch_half_busy_time_s)                                     as tch_half_busy_time_s
                     , sum(tch_full_busy_time_s)                                     as tch_full_busy_time_s
                     , avg(data_integrity)                                           as data_integrity

                     , 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 m.date between date_int(1) and date_int(1)
--                   and n.region = 'PUMA'
--                   and n.site_id in ('AMB002')

                group by m.date
                       , r.kota_kabupaten)
select *
from master
order by kota_kabupaten, date
;

