Received: from sss.sss.pgh.pa.us (sss.pgh.pa.us [209.114.166.2]) by hub.org (8.9.3/8.9.3) with ESMTP id XAA78399 for ; Wed, 10 Nov 1999 23:00:59 -0500 (EST) (envelope-from tgl@sss.pgh.pa.us) Received: from sss.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.sss.pgh.pa.us (8.9.1/8.9.1) with ESMTP id XAA13559; Wed, 10 Nov 1999 23:00:16 -0500 (EST) To: Stuart Rison cc: marten@feki.toppoint.de, pgsql-sql@postgresql.org Subject: Re: [SQL] HAVING in EXISTS-clause ... In-reply-to: Your message of Wed, 10 Nov 1999 11:03:45 +0000 (GMT) Date: Wed, 10 Nov 1999 23:00:16 -0500 Message-ID: <13557.942292816@sss.pgh.pa.us> From: Tom Lane Stuart Rison writes: > It's just a case of reversing the order of the aggregate and the constant > in your qualifier (as hinted to by the error message). > This worked for me > SELECT [DISTINCT] AO,AT,AV FROM P3AT AS OAT > WHERE > EXISTS( > SELECT AO FROM P3AT > WHERE > AO = OAT.AO > GROUP BY AO > HAVING 8 = COUNT(*) > ); Marten has actually stumbled across a bug here: the rewriter mistakenly thinks that COUNT() appears in the outer SELECT's WHERE clause (well, it does, but since it's inside a sub-select it really belongs to the inner SELECT) and is trying to rewrite the query to push the COUNT() into yet a third level of SELECT. That doesn't hurt in this case, but in other cases it generates the wrong answer. The "must be at right side in qual" message appears because the rewriter only knows how to push down COUNT() on the right side of an operator. That's a pretty bogus limitation, but the whole thing is really pretty bogus --- none of that rewriter code would exist if we could handle views involving aggregate functions as sub-selects in FROM. So it's probably not worth any effort to add more code to a routine that shouldn't exist in the first place; we've got to work on the fundamental problem instead. regards, tom lane