Q:

How to avoid duplicating a function call in this JPA/JPQL query?

I have the following JPA query that truncates dates to a full hour and counts them:
SELECT a.alertconfiguration.id.id,
       date_trunc('hour', a.date) AS fromDate,
       count(*)                   AS count
FROM   alert a
GROUP  BY a.alertconfiguration.id.id,
          a.alertlevel,
          date_trunc('hour', a.date)

I'm running this in a Spring Boot application using Hibernate. It works fine. But I don't want to duplicate the function call to date_trunc.
I have tried referring to fromDate in the GROUP BY clause but then I get an exception org.postgresql.util.PSQLException: ERROR: column "fromdate" does not exist
http://hibernate.atlassian.net/browse/HHH-9301 also states it is not possible to refer to aliases in the group by clause.
How could I rewrite my query without the duplicate function call?

A:

Can you give a try using 2 instead of date_trunc('hour', a.date) in group by clause, as fromdate is 2nd column

