From: Martin Lillepuu <martin@lillepuu.com>
To: pgsql-sql@postgresql.org
Subject: union & subqueries
Date: Fri, 02 Mar 2001 15:42:43 +0200
Message-ID: <3A9FA353.EE681533@lillepuu.com> (raw)
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 AT
WHERE AT.trans_id = ar.id
AND AT.amount > 0
AND AT.amount=-ar.amount
AND 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 AT
WHERE AT.trans_id = ap.id
AND AT.amount < 0
AND AT.amount=-ap.amount
AND 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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-sql@postgresql.org
Cc: martin@lillepuu.com
Subject: Re: union & subqueries
In-Reply-To: <3A9FA353.EE681533@lillepuu.com>
References: <3A9FA353.EE681533@lillepuu.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox