Received: from localhost (maia-4.hub.org [200.46.204.183]) by postgresql.org (Postfix) with ESMTP id 771D69FBD39 for ; Wed, 13 Jun 2007 10:58:50 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 73610-02 for ; Wed, 13 Jun 2007 10:58:42 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.5 Received: from mtai05.charter.net (mtai05.charter.net [209.225.8.185]) by postgresql.org (Postfix) with ESMTP id 8FCDF9FBD34 for ; Wed, 13 Jun 2007 10:58:41 -0300 (ADT) Received: from aa04.charter.net ([10.20.200.156]) by mtai05.charter.net (InterMail vM.7.08.02.00 201-2186-121-20061213) with ESMTP id <20070613135836.RBAP1566.mtai05.charter.net@aa04.charter.net>; Wed, 13 Jun 2007 09:58:36 -0400 Received: from [10.0.1.2] (really [66.188.164.45]) by aa04.charter.net with ESMTP id <20070613135836.VPJG26958.aa04.charter.net@[10.0.1.2]>; Wed, 13 Jun 2007 09:58:36 -0400 In-Reply-To: <466FEEC9.4050300@lake-lotawana.mo.us> References: <466FEEC9.4050300@lake-lotawana.mo.us> Mime-Version: 1.0 (Apple Message framework v752.2) Content-Type: text/plain; charset=US-ASCII; delsp=yes; format=flowed Message-Id: Cc: pgsql-sql@postgresql.org Content-Transfer-Encoding: 7bit From: Michael Glaesemann Subject: Re: join problem Date: Wed, 13 Jun 2007 08:58:30 -0500 To: A. R. Van Hook X-Mailer: Apple Mail (2.752.2) X-Chzlrs: 0 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200706/91 X-Sequence-Number: 28581 On Jun 13, 2007, at 8:19 , A. R. Van Hook wrote: > I have join problem: > "select i.ivid, v.eventdate, v.deposit, v.invdate, cai.db, > sum(i.tax) as tax, > sum(i.tax + i.rowtot) as totalP, > (sum(i.tax + i.rowtot) - v.deposit) as balance > from invoice v > left outer join > invoiceitems i > on v.ivid = i.ivid > where v.cusid = $cusid > and v.cusid = cai.cusid group > by i.ivid, v.eventdate, v.deposit, v.invdate, cai.db > ERROR: missing FROM-clause entry for table "cai" > > If I add cai to the from clause "from invoice v, cai, I get > ERROR: missing FROM-clause entry for table "cai" > ERROR: invalid reference to FROM-clause entry for table "v" I think you may need to change the order of the JOIN clause. Does this work? SELECT i.ivid , v.eventdate , v.deposit , v.invdate , cai.db , sum(i.tax) as tax , sum(i.tax + i.rowtot) as totalP , (sum(i.tax + i.rowtot) - v.deposit) as balance FROM cai JOIN invoice v ON (cai.cusid = v.cusid) LEFT JOIN invoiceitems i ON (v.ivid = i.ivid) WHERE v.cusid = $cusid GROUP BY i.ivid , v.eventdate , v.deposit , v.invdate , cai.db Note I've also moved the cai.cusid = v.cusid into the JOIN condition (which is what it is). Also, if cai doesn't have a ivid column and invoiceitems doesn't have a cusid column, you can use USING (cusid) and USING (ivid) rather than ON (cai.cusid = v.cusid) and ON (v.ivid = i.ivid), which has the nice property of outputing only one join column rather than one column for each table, (i.e., only one cusid column rather than one each for cai and invoice). Michael Glaesemann grzm seespotcode net