Sorry, the query of course should have been:
Code:
select a1.serviceKey, count(a1.id), sum(a1.total)
from TransactionItemDAO a1
where a1.transaction.typeCode=0
group by a1.serviceKey
order by sum(a1.total) desc
Anyway, that's not the fundamental problem. The problem is ORDER BY clause cannot directly contain aggregate function (at least MySQL doesn't accept it) and it is possible only in the form of alias - in bare SQL would be something like that (little bit simplified):
Code:
select a1.serviceKey, count(a1.id), sum(a1.total) as sumAlias
from transaction_item as a1
where a1.code=0
group by a1.serviceKey
order by sumAlias desc
This works with SQL, but HQL doesn't allow aliases for retrieved data, so the following query:
Code:
select a1.serviceKey, count(a1.id), sum(a1.total) as sumAlias
from TransactionItemDAO a1
where a1.transaction.typeCode=0
group by a1.serviceKey
order by sumAlias desc
throws exception (see the first post). Is there any solution to this problem?
Best regards
Martin Stepanek