union & subqueries - Mailing list pgsql-sql

From Martin Lillepuu
Subject union & subqueries
Date
Msg-id 3A9FA353.EE681533@lillepuu.com
Whole thread Raw
Responses Re: union & subqueries  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-sql
hello,

when I run following sql in 2 separate queries, they work fine. but when
used as one beg statement with UNION, I get following error:
join_references: variable not in subplan target lists

If I remove subqueries, they also work fine.

I'm currently using postgres 7.0.2. would upgrading to 7.0.3 or 7.1 beta
fix this? or is there a workaround for current version?

Query is supposed to get all AP/AR transactions with related
customer/vendor records and transactions expense/income account name
(with subquery). 

---- 8< ---- 8< ----

SELECT ar.id, ar.invnumber as source, ar.notes, customer.name,
customer.addr1, customer.addr2, customer.addr3,
acc_trans.transdate, ar.invoice, acc_trans.amount,
(SELECT C.description FROM chart C, acc_trans ATWHERE AT.trans_id = ar.idAND AT.amount > 0AND AT.amount=-ar.amountAND
C.accno=AT.accno
) as desc,
'ar' as type
FROM ar, acc_trans
WHERE acc_trans.accno IN (1061,1065)
AND acc_trans.trans_id = ar.id
AND ar.customer = customer.id

UNION

SELECT ap.id, ap.ordnumber as source, ap.notes, vendor.name,
vendor.addr1, vendor.addr2, vendor.addr3,
acc_trans.transdate, ap.invoice, acc_trans.amount,
(SELECT C.description FROM chart C, acc_trans ATWHERE AT.trans_id = ap.idAND AT.amount < 0AND AT.amount=-ap.amountAND
C.accno=AT.accno
) as desc,
'ap' as type
FROM ap, acc_trans
WHERE acc_trans.accno IN (1061,1065)
AND acc_trans.trans_id = ap.id
AND ap.vendor = vendor.id
ORDER BY transdate

---- 8< ---- 8< ----

--
Martin Lillepuu | E-mail: martin@lillepuu.com | GSM: 051 56 450


pgsql-sql by date:

Previous
From: Laurent
Date:
Subject: lo_import for storing Blobs
Next
From: Hannu Krosing
Date:
Subject: Re: [HACKERS] why the DB file size does not reduce when 'delete' the data in DB?