-->
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.  [ 2 posts ] 
Author Message
 Post subject: Multiple "count" statements
PostPosted: Wed Jun 09, 2004 4:04 am 
Newbie

Joined: Wed Mar 24, 2004 6:18 am
Posts: 15
Hi! I would like to extract more than one count from a single table with different criteria. In SQL I would use scalar subqueries (subqueries inside the "select" statement) or unions, but none of them seems to be supported by Hibernate. Is there an alternative or I'm forced to use several queries or native SQL? Maybe with self-joins?

What I try to do, following the examples in the HQL docs, is something like this: select the name of the cat, the number of this cat's kittens whose weight is over 10 pounds and the number of kittens whose weight is between 7 and 9 pounds.

Thanks in advance


Top
 Profile  
 
 Post subject: Multiple count statements from one table
PostPosted: Wed Jun 08, 2005 12:05 am 
Beginner
Beginner

Joined: Tue Jun 07, 2005 11:36 pm
Posts: 22
A bit freaky that almost exactly one year after this post, I am trying to do the same thing. Is there anything new with Hibernate 3 that would make this possible? I have gotten it to work with native SQL using "from subqueries," but I know that HQL doesn't support that.

I come close with this rather unorthodox query (assuming two tables Family and Kids):

select family.name, count(distinct all_kids), count(distinct teen)
from Family family
left join family.kids as all_kids
left join family.kids as teen
where teen.age > 12

I get the teen count right, but not all_kids. I have gotten the correct counts with this query (yes, I know it is bad):

select count(distinct all_kids), count(distinct teen)
from Kids as all_kids, Kids as teen
where teen.age > 12

But the performance is just abysmal - tens of seconds for even a very small DB.

Any new thoughts in the past year?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.