Robin Sander commented on an issue
Hibernate ORM / Bug HHH-6385
Order by avg() does not use function registered by dialect (e.g. AvgWithArgumentCastFunction)
Some dialects register a special function for the aggregate function 'avg'. For example, HSQLDialect calls {{registerFunction("avg", new AvgWithArgumentCastFunction("double"))}}
which is used if {{avg}} appears in a select statement but not if {{avg}} appears in an {{order by}} statement which leads to wrong results.

For exampe, the following query
{code...