DROP TABLE tmp; DROP TABLE tmp2;
CREATE TABLE tmp AS SELECT DISTINCT trans_id FROM acc_trans WHERE chart_id IN (SELECT fldvalue::integer FROM defaults WHERE fldname='fxgain_accno_id');
CREATE TABLE tmp2 AS SELECT trans_id FROM tmp WHERE trans_id IN (SELECT DISTINCT trans_id FROM acc_trans WHERE chart_id IN (SELECT fldvalue::integer FROM defaults WHERE fldname='fxloss_accno_id'));
SELECT invnumber FROM ar WHERE id IN (SELECT trans_id FROM tmp2); SELECT invnumber FROM ap WHERE id IN (SELECT trans_id FROM tmp2);