Thanks a lot Tom.
I'll solve it with a subquery.

Carlos Sotto Maior (UOL)
csotto@uol.com.br

 

 

De: "Tom Lane" <tgl@sss.pgh.pa.us>
Enviada: 2021/01/30 19:46:37
Para: csotto@uol.com.br
Cc: pgsql-bugs@lists.postgresql.org
Assunto: Re: Use of HAVING (Select/Group By) on a output-name of an aggregate function causes SYNTAX ERROR
 
"Carlos Sotto Maior \(UOL\)" <csotto@uol.com.br> writes:
> In other words: Postgresql is matching <HAVING> arguments to columns at
> table level instead of matching to a group by result level.

That's per SQL standard. The fact that we allow the case in
GROUP BY is an anachronism. (You'll find also that we only allow it
for GROUP BY items that are *just* an output column name, not any
more-complicated expression. The equivalent restriction for HAVING
would make such a feature near useless anyway.)

regards, tom lane