Thread: Re: [SQL] sum-function

Re: [SQL] sum-function

From
Tom Lane
Date:
guenther@laokoon.IN-Berlin.DE (Christian Guenther) writes:
> SELECT (sum(case when dict.word = 'museum' then 1 else 0 end)) as qu, 
> dict.url_id FROM dict, url
>              WHERE url.rec_id = dict.url_id
>                    AND url.url LIKE '%%'
>                    AND dict.word in ('museum')
>              GROUP BY dict.url_id
>         having qu > 0
> The Problem is qu, I cannot use it for the having.

Neither WHERE nor HAVING pay any attention to column labels; they
work on original attributes.  (Only GROUP BY and ORDER BY accept
column labels as substitutes for real expressions.)  You'll need
to repeat the sum() expression in HAVING, I'm afraid.
        regards, tom lane