select R.*, CAST(NVL(Expended,0) as I) as expended from rta_elements R LEFT JOIN ( select sum(amount) as Expended, val(substr(sponsor_id,5,2)) as GroupID ; from tip ; inner join ledger on ledger.tip_id = tip.tip_id and source="RTA" ; group by 2) MyAmount ON R.ElementNum = myAmount.GroupIDI believe this is what you actually wanted, correct?