Re: Use of HAVING (Select/Group By) on a output-name of an aggregate function causes SYNTAX ERROR - Mailing list pgsql-bugs

From Magnus Hagander
Subject Re: Use of HAVING (Select/Group By) on a output-name of an aggregate function causes SYNTAX ERROR
Date
Msg-id CABUevEz6rfHg2w1AWFM8HHK0zpuy_Q-BzudPRO8O=H2CL0k-Aw@mail.gmail.com
Whole thread Raw
In response to Re: Use of HAVING (Select/Group By) on a output-name of an aggregate function causes SYNTAX ERROR  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-bugs
On Sun, Jan 31, 2021 at 1:01 AM Carlos Sotto Maior (UOL)
<csotto@uol.com.br> wrote:
>
> Thanks a lot Tom.
> I'll solve it with a subquery.

No need for a subquery. In your particular case,  it should just work with:

SELECT count(*) as cnt, f1, f2, active
                FROM public.z_having
                GROUP BY f1, f2, active
                HAVING active = True AND count(*) > 1
                ORDER BY cnt DESC


It can get ugly if your aggregates are large and complex, but not for
a simple count(*)...

//Magnus



pgsql-bugs by date:

Previous
From: PG Bug reporting form
Date:
Subject: BUG #16846: "retrieved too many tuples in a bounded sort"
Next
From: Tom Lane
Date:
Subject: Re: BUG #16846: "retrieved too many tuples in a bounded sort"