pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Stuart Rison <rison@biochemistry.ucl.ac.uk>
To: marten@feki.toppoint.de
Cc: pgsql-sql@postgreSQL.org
Subject: Re: [SQL] HAVING in EXISTS-clause ...
Date: Wed, 10 Nov 1999 11:03:45 +0000 (GMT)
Message-ID: <Pine.LNX.4.10.9911101046540.5520-100000@bsmlx17> (raw)
In-Reply-To: <199911091126.MAA27023@feki.phoenix-edv.netzservice.de>

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(*)
   );

I put the DISTINCT in square brackets to indicate that it is optional.
The statement will select all rows from sets with 8 indentical
AOs.  Without the DISTINCT, all such instances will be returned, with the
DISTINCT, only row with different combinations of AO, AT and AV are
returned.  Note that with the DISTINCT you have no way of indentifying
which exact row has been returned from set with identical AO,AT and AV
combinations.

As an aside, is it worth changing the error message to something like:

"ERROR:  rewrite: aggregate column of view must be at rigth side in qual.
Try exchanging the position of the aggregate with the value it is compared
to."

or should we in fact avoid such error messages? 

Regards,

S. 

On Tue, 9 Nov 1999 marten@feki.toppoint.de wrote:

> Hello PostgreSQL user,
> 
> we're haveing some trouble when doing the following commands:
> 
> a) This statement works very well ...
> 
> SELECT AO,AT,AV FROM P3AT
> GROUB BY AO
> HAVING COUNT(*)>1
> 
> 
> b) This statement works very well ...
> 
> SELECT AO,AT,AV FROM P3AT AS OAT 
> WHERE 
>  EXISTS(
>    SELECT AO FROM P3AT
>    WHERE
>      AO = OAT.AO
>    GROUB BY AO)
> 
> c) This statement does NOT work. It gives an error message:
>    "rewrite: aggregate column of view must be ar rigth side in qual"
> 
> SELECT AO,AT,AV FROM P3AT AS OAT
> WHERE
>  EXISTS(
>    SELECT AO FROM P3AT
>    WHERE
>      AO = OAT.AO
>    GROUP BY AO
>    HAVING COUNT(*) = 8)
> 
> The meaning of the statement is: return the result as triples, but
> make sure, that you return only these results, where you have 
> eight result rows available for each AO.
> 
> Perhaps someone may help me !?
> 
> Marten
> 
> 
> 
>  
> 
> 
> ************
> 

Stuart C. G. Rison
Department of Biochemistry and Molecular Biology
6th floor, Darwin Building, University College London (UCL)
Gower Street, London, WC1E 6BT, United Kingdom
Tel. 0207 504 2303, Fax. 0207 380 7193
e-mail: rison@biochem.ucl.ac.uk




view thread (5+ messages)  latest in thread

Message-ID: <Pine.LNX.4.10.9911101046540.5520-100000@bsmlx17>
Permalink:  ../Pine.LNX.4.10.9911101046540.5520-100000@bsmlx17/
Also on:    postgresql.org/message-id/Pine.LNX.4.10.9911101046540.5520-100000@bsmlx17

 · 

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: rison@biochemistry.ucl.ac.uk, marten@feki.toppoint.de
  Subject: Re: [SQL] HAVING in EXISTS-clause ...
  In-Reply-To: <Pine.LNX.4.10.9911101046540.5520-100000@bsmlx17>
  References: <199911091126.MAA27023@feki.phoenix-edv.netzservice.de> <Pine.LNX.4.10.9911101046540.5520-100000@bsmlx17>

* 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