Received: from localhost (maia-1.hub.org [200.46.204.191]) by postgresql.org (Postfix) with ESMTP id 0932B9FB3D8 for ; Mon, 23 Jul 2007 08:57:28 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.191]) (amavisd-maia, port 10024) with ESMTP id 61789-07 for ; Mon, 23 Jul 2007 08:57:24 -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 D85C09FA4DB for ; Mon, 23 Jul 2007 08:57:24 -0300 (ADT) Received: from list by ciao.gmane.org with local (Exim 4.43) id 1ICwXT-00005V-EE for pgsql-sql@postgresql.org; Mon, 23 Jul 2007 13:57:23 +0200 Received: from cpe.atm2-0-1261189.0x503fa7b2.bynxx8.customer.tele.dk ([80.63.167.178]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Mon, 23 Jul 2007 13:57:23 +0200 Received: from nis by cpe.atm2-0-1261189.0x503fa7b2.bynxx8.customer.tele.dk with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Mon, 23 Jul 2007 13:57:23 +0200 X-Injected-Via-Gmane: http://gmane.org/ To: pgsql-sql@postgresql.org From: =?ISO-8859-1?Q?Nis_J=F8rgensen?= Subject: Re: The nested view from hell - Restricting a subquerry Date: Mon, 23 Jul 2007 13:57:11 +0200 Lines: 64 Message-ID: References: <46A2CABF.3080108@obviously.com> <2385.1185127507@sss.pgh.pa.us> <87y7h74un7.fsf@oxford.xeocode.com> <87d4yjmkiw.fsf@oxford.xeocode.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: cpe.atm2-0-1261189.0x503fa7b2.bynxx8.customer.tele.dk User-Agent: Thunderbird 1.5.0.12 (Windows/20070509) In-Reply-To: <87d4yjmkiw.fsf@oxford.xeocode.com> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200707/134 X-Sequence-Number: 28869 Gregory Stark skrev: >> 1. Look up all order_ids for which (order_id,my_invoice_id) appear in >> eg_orders >> >> 2. Find all rows (in both branches of the UNION) with these id_s >> >> 3. Group the rows, and calculate max(invoice_id) >> >> 4. Filter the result rows on max(invoice_id) = my_invoice_id. > > So here's a hypothetical data set for which this algorithm fails: > > order_id invoice_id mileage > -------------------------------------------- > 1 1 100 > 1 2 100 > > Your algorithm would produce > > order_id max(invoice_id) sum(mileage) > -------------------------------------------- > 1 1 100 > > Whereas the correct output would be to output no records at all. (I assume you are using "1" as the parameter to the query). You seem to have interpreted one or more of the steps differently than I intended them. I have tried to clarify (resorting to SQL for the first step). 1. SELECT DISTINCT order_id FROM eg_order WHERE invoice_id = my_invoice_id 2. Find all rows (in both branches of the UNION) with these order_ids. 3. Group the rows on order_id, and calculate max(invoice_id) 4. Filter the result rows on max(invoice_id) = my_invoice_id. Thus, step 1 produces order_id ---------- 1 step 2 produces order_id invoice_id mileage -------------------------------------------- 1 1 100 1 2 100 Step 3 produces order_id max(invoice_id) sum(mileage) -------------------------------------------- 1 2 200 Step 4 eliminates this row, since it does not satisfy the criteria. Nis