Anyway "select avg(vote.rating) from Vote vote" will return 1 row, and sum of 1 row has no sense !
Main Topics
Browse All TopicsHow can I modify an HQL Query to sum the result of a subquery? For example, The query below gives me a list of average ratings, but what I would like to do is get the sum of that list, and every attempt has been met with org.hibernate.hql.ast.Quer
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
This should return 1 value:
select avg(vote.rating) from Vote vote
Since you have no group by statement, that will average all rating values for the entire table together; therefore, wouldn't make sense to need a sum after the fact.
Don't believe you can use two aggregates together, so your derived table looks like the right approach from a SQL syntax perspective. Just have to alias the derived table -- at least that would be the issue if running in against database directly. May have other issues in HQL.
select sum(t.averages) from (select avg(vote.rating) as averages from Vote vote) as t
Sorry I had omitted the groupBy clause to try and make it simple, but you are right without it this particular example doesn't make sense. The actual query will have the groupby clause.
I tried your alias idea in the HQL and crossed my fingers, but I am still seeing the unexpected token exception. Here is the full query I tried:
select sum(t.averages) from (
select avg(vote.rating) as averages from Vote vote, Story story where story.id = vote.objectId group by vote.objectId ) as t
But that is the point. If you average all the ratings, you will get one number.
rating1 = 100
rating2 = 75
rating3 = 90
rating4 = 85
Result of your initial SQL query should be 87.5.
Taking a sum of that gives you 87.5. Doesn't make sense to do that unless you are getting multiple values somehow which would mean your initial query is not as simple as shown which may contribute to the error you are getting; therefore, please post actual query.
Otherwise, as stated, I know there are issues with using derived table without giving it an alias as I showed.
That looks ok from a SQL syntax perspective aside from the join statement I would suggest using ANSI compliant join.
select sum(t.averages) as sum_of_averages
from (
select avg(vote.rating) as averages
from Vote vote
inner join Story story on story.id = vote.objectId
group by vote.objectId
) as t
Will have to take a step back and think about what the HQL error message you are getting is referring to with invalid token message.
HQL doesn't support the equivalent of a nested table expression that is available in most SQL databases. You'll have to execute a SQL statement via the createSQLQuery method from the hibernate Session object. If you stick with SQL-92 SQL syntax you should be fine supporting multiple databases if this is why you have to use HQL.
More info at the following link:
http://docs.jboss.org/hibe
Christian.
Business Accounts
Answer for Membership
by: louislietaerPosted on 2009-04-29 at 12:44:21ID: 24263991
Are you sure you want the sum of averages, if not
select sum(vote.rating) from Vote Vote