-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 9 posts ] 
Author Message
 Post subject: Ordering by aggregate functions using HQL
PostPosted: Wed Aug 27, 2003 11:17 am 
Newbie

Joined: Wed Aug 27, 2003 10:39 am
Posts: 4
Is it possible? I use the GROUP BY clause and select some counts, sums etc. I need to order the list according to these values.

I've tried query in the form
SELECT count(x) FROM t GROUP BY y ORDER BY count(x)
but I've got
java.sql.SQLException: General error: Invalid use of group function

After that I've tried following query:
SELECT count(x) as cnt FROM t GROUP BY y ORDER BY cnt
but I've got
net.sf.hibernate.QueryException: , expected in SELECT

Is there any way how fix up this?

Thanx for any answer...
Martin Stepanek


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 27, 2003 1:56 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Yes it is possible, but the queries you have shown are certainly not valid HQL.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 28, 2003 4:57 am 
Newbie

Joined: Wed Aug 27, 2003 10:39 am
Posts: 4
Thanx for you reply. I can see the queries don't work, but can you please provide me with the correct HQL for this task? I coudln't find it anywhere in the documentation or on the web.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 28, 2003 5:35 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
This is covered in the Hibernate documentation. Please read the "Hibernate Query Language" section.


You might also like to look in the src/net/sf/hibernate/test directory.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 28, 2003 8:52 am 
Newbie

Joined: Wed Aug 27, 2003 10:39 am
Posts: 4
ok, there is such an example in the documentation:

select order.id, sum(price.amount), count(item)
from Order as order
join order.lineItems as item
join item.product as product,
Catalog as catalog
join catalog.prices as price
where order.paid = false
and order.customer = :customer
and price.product = product
and catalog.effectiveDate < sysdate
and catalog.effectiveDate >= all (
select cat.effectiveDate
from Catalog as cat
where cat.effectiveDate < sysdate
)
group by order
having sum(price.amount) > :minAmount
order by sum(price.amount) desc

I've composed much simplier query according to this example:

select count(a1.id), sum(a1.total)
from TransactionItemDAO a1
where a1.transaction.typeCode=0
group by a1.serviceKey
order by sum(a1.total) desc

but after all, I get the same exception:
java.sql.SQLException: General error: Invalid use of group function

Is there any solution to this?

Thanks
Martin Stepanek


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 28, 2003 8:54 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Quote:
select count(a1.id), sum(a1.total)
from TransactionItemDAO a1
where a1.transaction.typeCode=0
group by a1.serviceKey
order by sum(a1.total) desc


Why do you want to group by a property you are not selecting? You already grouped with two aggreate functions (count and sum), there is no other value to group by.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 28, 2003 9:11 am 
Newbie

Joined: Wed Aug 27, 2003 10:39 am
Posts: 4
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


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 28, 2003 4:09 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
I think someone already took this issue to the MySQL team and they were considering fixing MySQL ... not sure what came out of this tho'.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 10, 2007 1:33 pm 
Newbie

Joined: Fri Apr 06, 2007 8:31 pm
Posts: 7
Hi

I am having problem with group by.
I saw the below example in the docs.

select cat.color, sum(cat.weight), count(cat)
from Cat cat group by cat.color

Does the queryobject return Cat objects with sum and count as well? How do we loop..When I loop and typecast to Cat, it gives classcast exception.

I am using

<property name="noOfCats" update="false" insert="false" formula="select count(b.cat) from CAT b where b.COLOR = COLOR"/>

Here CAT is DB table name and COLOR the db column name.

Is this correct? I am getting

I have added getters and setters for "noOfCats" in my java class.

Is there a complete example with mapping file and java code showing this?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 9 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.