(select apt_code,(select ap_name from id_ap_master where ap_code=apt_code and rownum=1) ap_Name,
0 apt_period,'0-Opening' Trans_Type, sum(APT_LOCAL_AMOUNT) Amount from id_ap_trans where apt_doc_date<'01-JAN-2018' group by apt_code
union all
select apt_code,(select ap_name from id_ap_master where ap_code=apt_code and rownum=1) ap_Name,
apt_period,'1-Services Obtained' Trans_Type, sum(APT_LOCAL_AMOUNT) Amount from id_ap_trans where  apt_doc_date between '01-JAN-2018'
and '31-DEC-2018' and apt_type in('SV','RSV','PIN','RPIN','ACN','XCR', 'XDR','ADM','ADN','RXCR','RXDR','PDR','TACN','TADN','RPDR','RPCR','PCR','PTV') group by apt_code, apt_period
union all
select apt_code,(select ap_name from id_ap_master where ap_code=apt_code and rownum=1) ap_Name,
apt_period,'4-Journals Passed' Trans_Type, sum(APT_LOCAL_AMOUNT) Amount from id_ap_trans where  apt_doc_date between '01-JAN-2018'
and '31-DEC-2018' and apt_type in('GLJV') group by apt_code, apt_period
union all
select apt_code,(select ap_name from id_ap_master where ap_code=apt_code and rownum=1) ap_Name,
apt_period,'5-Receivable Side Trans' Trans_Type, sum(APT_LOCAL_AMOUNT) Amount from id_ap_trans where  apt_doc_date between '01-JAN-2018'
and '31-DEC-2018' and apt_type in('RCR2','RCR', 'RIN', 'BRV','RBRV') group by apt_code, apt_period
union all
select apt_code,(select ap_name from id_ap_master where ap_code=apt_code and rownum=1) ap_Name,
apt_period,'2-Payments Done' Trans_Type, sum(APT_LOCAL_AMOUNT) Amount from id_ap_trans where  apt_doc_date between
'01-JAN-2018' and '31-DEC-2018' and apt_type in('BPV','BPVS', 'RBPV','CPVS','CPV','PREV','GLJV') group by apt_code, apt_period
union all
select apt_code,(select ap_name from id_ap_master where ap_code=apt_code and rownum=1) ap_Name,
apt_period,apt_type Trans_Type, sum(APT_LOCAL_AMOUNT) amount from id_ap_trans where  apt_doc_date between
'01-JAN-2018' and '31-DEC-2018' and apt_type  not in('SV','RSV','PIN','RPIN','ACN','XCR', 'XDR','RXCR','RXDR','BPV','BPVS', 'RBPV','CPVS','CPV','PREV','ADM','ADN','PDR','TACN','TADN','RPDR','RPCR','PCR','PTV','GLJV','RCR2','RCR','RIN','BRV','RBRV')
group by apt_code,apt_period,apt_type)
order by apt_code, apt_period
