Check if AR and acc_trans are in sync: select ar.invnumber, ac.trans_id, sum(ac.amount) from acc_trans ac join ar on (ar.id = ac.trans_id) where chart_id in (select id from chart where link='AR') and ar.amount = ar.paid group by ar.invnumber, ac.trans_id having round(sum(ac.amount)::numeric, 2) <> 0; Check if AP and acc_trans are in sync: select ap.invnumber, ac.trans_id, sum(ac.amount) from acc_trans ac join ap on (ap.id = ac.trans_id) where chart_id in (select id from chart where link='AP') and ap.amount = ap.paid group by ap.invnumber, ac.trans_id having round(sum(ac.amount)::numeric, 2) <> 0;