Received: from localhost (maia-4.hub.org [200.46.204.183]) by postgresql.org (Postfix) with ESMTP id B3F729FA265 for ; Mon, 30 Jul 2007 02:30:52 -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 23088-05 for ; Mon, 30 Jul 2007 02:30:45 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.5 Received: from ciao.gmane.org (main.gmane.org [80.91.229.2]) by postgresql.org (Postfix) with ESMTP id 9DC6B9FA25B for ; Mon, 30 Jul 2007 02:30:44 -0300 (ADT) Received: from list by ciao.gmane.org with local (Exim 4.43) id 1IFNq5-0002LZ-Fi for pgsql-sql@postgresql.org; Mon, 30 Jul 2007 07:30:41 +0200 Received: from 2109ds1-taa.0.fullrate.dk ([90.184.133.88]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Mon, 30 Jul 2007 07:30:41 +0200 Received: from nis by 2109ds1-taa.0.fullrate.dk with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Mon, 30 Jul 2007 07:30:41 +0200 X-Injected-Via-Gmane: http://gmane.org/ To: pgsql-sql@postgresql.org From: =?ISO-8859-1?Q?Nis_J=F8rgensen?= Subject: Re: Migration from SQLite Help (Left Join) Date: Mon, 30 Jul 2007 07:30:18 +0200 Lines: 63 Message-ID: References: <173c46830707291928p1b701e31u444782ee3e439c96@mail.gmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Complaints-To: usenet@sea.gmane.org X-Gmane-NNTP-Posting-Host: 2109ds1-taa.0.fullrate.dk User-Agent: Thunderbird 1.5.0.12 (Windows/20070509) In-Reply-To: <173c46830707291928p1b701e31u444782ee3e439c96@mail.gmail.com> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200707/181 X-Sequence-Number: 28916 Mitchell Vincent skrev: > SELECT c.customer_id as customer_id,c.customer_number as customer_number, > c.customer_name as customer_name,c.customer_status as > customer_status,cat.category_name as category_name, > c.bill_state as bill_state, coalesce(ctots.balance_due, 0.00) as > balance FROM customers as c, > customer_categories as cat > left join > (Select customer_id as cid, coalesce(sum(balance_due),0) as balance_due > FROM invoice_master WHERE status = 'Pending' group by cid) ctots on > ctots.cid = c.customer_id > where cat.category_id = c.category_id AND customer_name > LIKE lower('%%') AND (c.customer_status = 'Terminated' OR > c.customer_status = 'Active' or c.customer_status = 'Inactive') > ORDER BY c.customer_number DESC LIMIT 25 The problem seems to be that you expect SELECT a FROM b,c LEFT JOIN d to be interpreted as SELECT a FROM (b CROSS JOIN c) LEFT JOIN d whereas it is translated by postgresql as SELECT a FROM b CROSS JOIN (c LEFT JOIN d) There are many ways to fix this - I would suggest moving the join condition into the FROM-clause: SELECT c.customer_id as customer_id,c.customer_number as customer_number, c.customer_name as customer_name,c.customer_status as customer_status,cat.category_name as category_name, c.bill_state as bill_state, coalesce(ctots.balance_due, 0.00) as balance FROM customers as c INNER JOIN customer_categories as cat ON cat.category_id = c.category_id LEFT JOIN (Select customer_id as cid, coalesce(sum(balance_due),0) as balance_due FROM invoice_master WHERE status = 'Pending' group by cid) ctots on ctots.cid = c.customer_id WHERE customer_name LIKE lower('%%') AND (c.customer_status = 'Terminated' OR c.customer_status = 'Active' or c.customer_status = 'Inactive') ORDER BY c.customer_number DESC LIMIT 25 In fact, I believe you could remove the subquery as well: SELECT c.customer_id as customer_id,c.customer_number as customer_number, c.customer_name as customer_name,c.customer_status as customer_status,cat.category_name as category_name, c.bill_state as bill_state, coalesce(sum(im.balance_due, 0.00) as balance FROM customers as c INNER JOIN customer_categories as cat ON cat.category_id = c.category_id LEFT JOIN invoice_master im ON im.status = 'Pending' AND im.cid = c.customer_id WHERE customer_name LIKE lower('%%') AND (c.customer_status = 'Terminated' OR c.customer_status = 'Active' or c.customer_status = 'Inactive') ORDER BY c.customer_number DESC LIMIT 25