-->
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: Join madness when using aggregate functions.
PostPosted: Wed Feb 22, 2006 1:55 pm 
Newbie

Joined: Thu Jul 21, 2005 6:30 pm
Posts: 5
Hibernate version:
3.1 (w/ annotations)

Name and version of the database you are using:
MySQL 4.1.10a

I am having issues with a having clause in a query. The query that I am trying to execute looks something like (trimmed for readability):

Code:
select c.claimNumber
from Claim c where
  ...
group by
  c.claimNumber
having
  min(c.lineItems.fromDate) = :fromdate and
  max(c.lineItems.toDate) = :todate and
  sum(c.lineItems.billed) : billed


The resulting query ends up like (again trimmed for readibility):
Code:
select claim0_.claim_number as col_0_0_ from claims claim0_, lineitems lineitems7_, lineitems lineitems8_, lineitems lineitems9_ where
claim0_.claim_number=lineitems7_.claim_number and
claim0_.claim_number=lineitems8_.claim_number and
claim0_.claim_number=lineitems9_.claim_number
group by
claim0_.claim_number
having
min(lineitems7_.from_date)=? and
max(lineitems8_.to_date)=? and
sum(lineitems9_.billed)=?


The problem is that because the query is joining my claims to my line items table 3 different times rather than just re-using the lineitems relation that was already established (lineitems7_). The query actually ends joining the lineitems table to itself 3 times. This completely screws up sums (and counts too I am sure). For example: In one query I am hitting 4 lines. Rather than getting a sum of 140.09 I am seeing a sum of 2241.41 (104.09 * 4 * 4).

What I would like to see is a query more like:
Code:
select claim0_.claim_number as col_0_0_ from claims claim0_, lineitems lineitems7_ where
claim0_.claim_number=lineitems7_.claim_number
group by
claim0_.claim_number
having
min(lineitems7_.from_date)=? and
max(lineitems7_.to_date)=? and
sum(lineitems7_.billed)=?


Is this a known issue (couldn't find anything in JIRA)? Maybe it's expected behavior (I hope not!)? Is there a workaround for this? For now I am probably going to go back to plain ole' SQL, but I would like to find a HQL solution if one exists. HQL is much easier to write and maintain.

Thanks,
Josh


Top
 Profile  
 
 Post subject: Never mind...
PostPosted: Wed Feb 22, 2006 5:21 pm 
Newbie

Joined: Thu Jul 21, 2005 6:30 pm
Posts: 5
Just in case anyone cares (and if I need to look this up later!), I found the solution.

You need to manually specify that you are doing a join. Something along the lines of:

Code:
select c.claimNumber
from Claim c left outer join c.lineItems as lines where
  ...
group by
  c.claimNumber
having
  min(lines.fromDate) = :fromdate and
  max(lines.toDate) = :todate and
  sum(lines.billed) :billed


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.