Re: [SQL] HAVING in EXISTS-clause ...

From: Stuart Rison <rison(at)biochemistry(dot)ucl(dot)ac(dot)uk>
To: marten(at)feki(dot)toppoint(dot)de
Cc: pgsql-sql(at)postgreSQL(dot)org
Subject: Re: [SQL] HAVING in EXISTS-clause ...
Date: 1999-11-10 11:03:45
Message-ID: Pine.LNX.4.10.9911101046540.5520-100000@bsmlx17
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

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(at)feki(dot)toppoint(dot)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(at)biochem(dot)ucl(dot)ac(dot)uk

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Sladewski, Joseph 1999-11-10 16:17:56 PostgreSQL Compliance
Previous Message marten 1999-11-09 11:26:21 HAVING in EXISTS-clause ...