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

Поиск
Список
Период
Сортировка
От Stuart Rison
Тема Re: [SQL] HAVING in EXISTS-clause ...
Дата
Msg-id Pine.LNX.4.10.9911101046540.5520-100000@bsmlx17
обсуждение исходный текст
Ответ на HAVING in EXISTS-clause ...  (marten@feki.toppoint.de)
Ответы Re: [SQL] HAVING in EXISTS-clause ...  (Tom Lane <tgl@sss.pgh.pa.us>)
Список 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
WHEREEXISTS(  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



В списке pgsql-sql по дате отправления:

Предыдущее
От: marten@feki.toppoint.de
Дата:
Сообщение: HAVING in EXISTS-clause ...
Следующее
От: "Sladewski, Joseph"
Дата:
Сообщение: PostgreSQL Compliance