-->
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.  [ 6 posts ] 
Author Message
 Post subject: HQL question
PostPosted: Sat Nov 29, 2008 8:48 pm 
Newbie

Joined: Sat Nov 29, 2008 8:35 pm
Posts: 5
I've been trying to figure out the right query syntax for this.

I have this object (simplified):

Code:
public class TestOutcome {
    TestRun testRun;
    Bug bug;
}

I want to query that returns all the Bugs for a particular testRun and how many TestOutcomes have that Bug associated with it.

This query returns each of the Bugs, but I don't know how to get the count along with it:
Code:
select distinct outc.bug from TestOutcome outc where outc.testRun = ?  and outc.bug is not null

If I try making it
Code:
select distinct outc.bug, count(outc.bug) ...
I get an error about not in aggregate function or group by.

I've also tried variations like
Code:
select count(outc.bug), outc.bug from TestOutcome outc where testRun = ? and outc.bug is not null group by outc.bug

but this gets the same error as well. I have tried a half dozen variations of these and can't quite seem to get it right, although I'm sure it's simple.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Nov 30, 2008 7:25 am 
Newbie

Joined: Sun Nov 30, 2008 6:23 am
Posts: 2
how about

select count(distinct outc.bug) from TestOutcome outc where testRun = ? and outc.bug is not null group by outc.bug

try it

_________________
Email:devliu1986@gmail.com


Top
 Profile  
 
 Post subject: not what I wanted
PostPosted: Sun Nov 30, 2008 9:43 am 
Newbie

Joined: Sat Nov 29, 2008 8:35 pm
Posts: 5
Nope, that just gives me a list of 1's. I can already get the total count of the bugs, but I can't seem to get the associated bugs along with it.

So the query I already posted
Code:
select distinct outc.bug from TestOutcome outc where outc.testRun = ?  and outc.bug is not null
returns each of the bugs that I want, but not the total count of TestOutcomes with those bugs.

If I do this
Code:
select count(outc.bug) from TestOutcome outc where testRun = ? and outc.bug is not null group by outc.bug
I get the total count of each of the bugs, (in this case 4,8,1,6) but I don't know which bug is associated with each total. So really, I want the results from query A and query B in the same query, but my attempts to combine them have been unsuccessful.

But if I add ", outc.bug" to the query B, so it's like this:
Code:
select count(outc.bug), outc.bug from TestOutcome outc where testRun = ? and outc.bug is not null group by outc.bug
then I get "SQLException: Not in aggregate function or group by clause"

If I leave off the "group by" in the last query and just do this:
Code:
select count(outc.bug), outc.bug from TestOutcome outc where testRun = ? and outc.bug is not null
then I still get "SQLException: Not in aggregate function or group by clause"

Code:
select count(outc.bug), distinct outc.bug from TestOutcome outc where testRun = ? and outc.bug is not null
gets me "QuerySyntaxException: unexpected token: , near line 1, column 23"

Anyway, what I'm trying to do seems like it should be really simple, but for whatever reason I can't get the syntax right. If somebody who knows the answer could reply it would be very appreciated.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 01, 2008 5:53 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
You need to use two queries. One for the count and one for the data. Or you can count the number of items returned by the "data" query.

Eg:

Code:
List<TestOutcome> result = query.list();
int count = result.size();


Top
 Profile  
 
 Post subject: too many queries?
PostPosted: Mon Dec 01, 2008 10:12 am 
Newbie

Joined: Sat Nov 29, 2008 8:35 pm
Posts: 5
I could do the distinct query and then do an additional query for each item, but that seems like way too many queries -- if I have 10 distinct items then I've have to do 1 + 10 queries or 11 scans of the same table(s). I'm surprised I can't do this all in one query that would prevent the redundancy. I still suspect there's a syntax that would do it for me. I thought it would be simple, but maybe I have to do a subquery or something more complicated?


Top
 Profile  
 
 Post subject: A solution, but with problems ordering
PostPosted: Mon Dec 01, 2008 12:40 pm 
Newbie

Joined: Sat Nov 29, 2008 8:35 pm
Posts: 5
I figured this out, here's the query that worked for me:

Code:
select distinct outc.bug,
(select count(outb) from cuanto.TestOutcome outb where outb.testRun = ? and outb.bug = outc.bug)
from cuanto.TestOutcome outc where outc.testRun = ? and outc.bug is not null


Now that I'm retrieving the correct values, I'd like to get them ordered descending by the count determined in the sub query, but again the syntax is elluding me. Here's what I tried, adding "as bugcount" and "order by bugcount" to the query:

attempt 1:
Code:
select distinct outc.bug,
(select count(outb) from cuanto.TestOutcome outb where outb.testRun = ? and outb.bug = outc.bug) as bugcount
from cuanto.TestOutcome outc where outc.testRun = ? and outc.bug is not null order by bugcount


attempt 2:
Code:
select distinct outc.bug,
(select count(outb) as bugcount from cuanto.TestOutcome outb where outb.testRun = ? and outb.bug = outc.bug)
from cuanto.TestOutcome outc where outc.testRun = ? and outc.bug is not null order by bugcount


but both of these result in "SQLException: Column not found: BUGCOUNT in statement..."


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