-->
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.  [ 3 posts ] 
Author Message
 Post subject: join 2 unrelated tables
PostPosted: Tue Aug 05, 2008 11:16 am 
Newbie

Joined: Tue Jul 22, 2008 11:30 am
Posts: 13
I’m trying to combine 2 queries into one but I’m not sure what the best way is.

Basically, I want to compare the sum of products from this week compared to last week.
Here are 2 sql queries that do what I need:

Code:
Select sum(amount) from products where productCategoryId = 10 and day=This week <--date range pseudo code

Select sum(amount) from products where productCategoryId = 10 and day=last week


I have a Product object that is mapped with hibernate and I have zero problems running these 2 queries separately.
What I want to do is combine the 2 like you would normal sql

Code:
Select sum(current.amount) as current_amount, sum(previous.amount) as previous_amount from products current,
products previous where
current.productCategoryId=10 and current.day=this week and
previous.productCategoryId=10 and previous.day=last week


Theta style joins looked promising and almost got what I needed. I forgot to account for a case when the last week requirement doesn’t exist.

So my actual query looks like:
Code:
select
sum(current.amount) as current_amount,
sum(previous.amount) as previous_amount
from   Products  current
left join Products previous  previous.productCategoryId=10 and previous.day=last week
where current.productCategoryId=10 and current.day=this week


This way, if the previous week doesn’t exist then the sum for the current week is still returned.
Is there an HQL equivalent for this? I’m not sure how to do it without the on keyword.

Im’ using Hibernate vs 3.2.6

Thanks,


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 05, 2008 2:06 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
If you have a WEEK() function in your database that extracts the week number from a date it should be possible to do something like:

Code:
select sum(amount), week(day)
from products
where productCategoryId=10
group by week(day)
having week(day) = this week or week(day) = last week


One difference is that you get two rows instead of one.

Doing arbitrary joins between tables is not possible in HQL. There is an issue in JIRA (http://opensource.atlassian.com/project ... wse/HHH-16) about this so maybe it will appear some time in the future.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 05, 2008 2:13 pm 
Newbie

Joined: Tue Jul 22, 2008 11:30 am
Posts: 13
Thanks for the update.

I saw JIRAissue but I was thrown off by the last post (that said it was fixed in HQL)

Thanks for help on the query as well. I was able to tweak things to get the results I needed.


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