Received: from magus.postgresql.org ([87.238.57.229]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TYfVE-0000yi-Vh for pgsql-performance@postgresql.org; Wed, 14 Nov 2012 16:07:49 +0000 Received: from relay4-d.mail.gandi.net ([217.70.183.196]) by magus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TYfVC-00072f-EI for pgsql-performance@postgresql.org; Wed, 14 Nov 2012 16:07:48 +0000 X-Originating-IP: 217.70.178.133 Received: from mfilter3-d.gandi.net (mfilter3-d.gandi.net [217.70.178.133]) by relay4-d.mail.gandi.net (Postfix) with ESMTP id BEA2B172092; Wed, 14 Nov 2012 17:07:34 +0100 (CET) X-Virus-Scanned: Debian amavisd-new at mfilter3-d.gandi.net Received: from relay4-d.mail.gandi.net ([217.70.183.196]) by mfilter3-d.gandi.net (mfilter3-d.gandi.net [10.0.15.180]) (amavisd-new, port 10024) with ESMTP id gCAybP13np0c; Wed, 14 Nov 2012 17:07:33 +0100 (CET) X-Originating-IP: 98.27.54.176 Received: from [192.168.10.146] (cpe-098-027-054-176.nc.res.rr.com [98.27.54.176]) (Authenticated sender: adsend@dunslane.net) by relay4-d.mail.gandi.net (Postfix) with ESMTPSA id DFEC5172086; Wed, 14 Nov 2012 17:07:32 +0100 (CET) Message-ID: <50A3C1C3.7000809@dunslane.net> Date: Wed, 14 Nov 2012 11:07:31 -0500 From: Andrew Dunstan User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:15.0) Gecko/20120827 Thunderbird/15.0 MIME-Version: 1.0 To: David Greco CC: "pgsql-performance@postgresql.org" Subject: Re: Poor performance using CTE References: <187F6C10D2931A4386EE8E58E13857F61291C600@BY2PRD0811MB415.namprd08.prod.outlook.com> <50A3BDE2.1050201@dunslane.net> <187F6C10D2931A4386EE8E58E13857F61291D872@BY2PRD0811MB415.namprd08.prod.outlook.com> In-Reply-To: <187F6C10D2931A4386EE8E58E13857F61291D872@BY2PRD0811MB415.namprd08.prod.outlook.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: -2.6 (--) X-Archive-Number: 201211/165 X-Sequence-Number: 48531 On 11/14/2012 10:56 AM, David Greco wrote: > You're right. I was translating an oracle query , but looks like PG will allow some syntax that is different. Trying to find entries in fedexinvoices where smp_pkg.get_invoice_charges(id) returns a record containing charge_name in ('ADDRESS CORRECTION CHARGE','ADDRESS CORRECTION'). Should return the fedexinvoices row and the row from smp_pkg.get_invoice_charges that contains the address correction. > > > Something like this, though this is syntactically incorrect as smp_pkg.get_invoice_charges returns a set: > > > select fedexinvoices.*, (smp_pkg.get_invoice_charges(id)).* > from fedexinvoices > WHERE > trim(fedexinvoices.trackno)='799159791643' > and > (smp_pkg.get_invoice_charges(id)).charge_name IN ('ADDRESS CORRECTION CHARGE','ADDRESS CORRECTION') First, please don't top-post when someone has replied underneath your post. It makes the thread totally unreadable. See You could do something like this: WITH invoices as ( select * from fedexinvoices where trim(fedexinvoices.trackno)='799159791643' ), charges as ( SELECT fi2.id, smp_pkg.get_invoice_charges(fi2.id) charge_info from fedexinvoices fi2 join invoices i on i.id = f12.id ) select invoices.* from invoices inner join charges on charges.id = invoices.id AND (charges.charge_info).charge_name IN ('ADDRESS CORRECTION CHARGE','ADDRESS CORRECTION') ; Or probably something way simpler but I just did this fairly quickly and mechanically cheers andrew