-->
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.  [ 7 posts ] 
Author Message
 Post subject: How HQL do "select count(*) from (select ...where...)&q
PostPosted: Thu Jan 25, 2007 10:08 pm 
Newbie

Joined: Tue Jan 17, 2006 9:48 pm
Posts: 16
Hi All,

HQL does not support sub query after from. I do not need this feature in most cases.
However, when I check total count of a report query, I have to write a hql like:
Code:
select count(*) from (select x.a, x.b, y.c from...where...)


Does hibernate provides any way to achieve it?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 25, 2007 10:18 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Is query.list().size() not sufficient? If not (e.g. if you want to get the count before doing the real query), then just use two different queries: a "select a, b, c where ...", and a "select count(*) where ...".

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 28, 2007 2:44 am 
Newbie

Joined: Tue Jan 17, 2006 9:48 pm
Posts: 16
Yes, I want get the count before doing the real query. However, "select count(*) where" can not meet all cases.
For example. there are three records:
1. a1, b1, c1
2. a1, b1, c2
3. a1, b2, c1
The count result I wanted is:
select count(*) from (Select distinct A, B, C) = 3
select count(*) from (Select distinct A, B) = 2
select count(*) from (Select distinct A) = 1


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 28, 2007 4:41 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
You will need separate queries for those. They'll look like "select count(distinct a, b, c) where ...", "select count(distinct a, b) where ...", etc.

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 28, 2007 9:17 pm 
Newbie

Joined: Tue Jan 17, 2006 9:48 pm
Posts: 16
Oh, does Hibernate support "select count(distinct a, b) from ... where ..."?
How to do it? I have tried in my application with Oracle.
count(distinct a) is okay. But count(distinct a, b) is illegal.
The error message is:
org.hibernate.hql.ast.QuerySyntaxException: expecting CLOSE, found ',' near line 1, column 32 [select count(distinct t.company, t.id) as mycount from ecv.dt.po.Topic t]


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 28, 2007 9:42 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Yep, I see that problem. Try it with group by: "select distinct c where ... group by a, b", "select distinct b where ... group by a", etc.

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 29, 2007 12:23 am 
Newbie

Joined: Tue Jan 17, 2006 9:48 pm
Posts: 16
Yeah, group by is another choice.
I will test the performance of group by count later. If it is acceptable, we will apply it.


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