pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Stuart Rison <rison@biochemistry.ucl.ac.uk>
Cc: marten@feki.toppoint.de
Cc: pgsql-sql@postgresql.org
Subject: Re: [SQL] HAVING in EXISTS-clause ...
Date: Wed, 10 Nov 1999 23:00:16 -0500
Message-ID: <13557.942292816@sss.pgh.pa.us> (raw)
In-Reply-To: <Pine.LNX.4.10.9911101046540.5520-100000@bsmlx17>

Stuart Rison <rison@biochemistry.ucl.ac.uk> 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



view thread (5+ messages)  latest in thread

Message-ID: <13557.942292816@sss.pgh.pa.us>
Permalink:  ../13557.942292816@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/13557.942292816@sss.pgh.pa.us

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-sql@postgresql.org
  Cc: tgl@sss.pgh.pa.us, rison@biochemistry.ucl.ac.uk, marten@feki.toppoint.de
  Subject: Re: [SQL] HAVING in EXISTS-clause ...
  In-Reply-To: <13557.942292816@sss.pgh.pa.us>
  References: <Pine.LNX.4.10.9911101046540.5520-100000@bsmlx17> <13557.942292816@sss.pgh.pa.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox