-->
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.  [ 4 posts ] 
Author Message
 Post subject: Selecting from a subquery
PostPosted: Thu Feb 07, 2008 1:03 pm 
Newbie

Joined: Thu Feb 07, 2008 12:55 pm
Posts: 2
I'm trying to write this in HQL:

select invitemid, sum(onOrder)
from (
select
invitemid,
jobid,
sum(case when typeid=1 then quantity else 0 end) -
sum(case when typeid=2 then quantity else 0 end) -
sum(case when typeid=8 then quantity else 0 end) as onOrder
from inv_transaction
where typeid in (1,2,8)
group by invitemid, jobid
) as jobOnOrder
where onOrder>0
group by invitemid

What I essentially want to do, is add up the details on each job (ordered-received-cancelled) and then ignore all those that are negative (sometimes inventory might be received that wasn't ordered) and sum up the rest, to find out how many are on order for a given item.

Via hibernate, it seems to not allow selects in a from(). I've been looking everywhere and asking the Hibernate guru here, to no avail. Is there any way to get around this?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 07, 2008 1:05 pm 
Expert
Expert

Joined: Mon Nov 26, 2007 2:29 pm
Posts: 443
I believe you can achieve that using nested Projections.
Check the reference documantation, 15.7. Projections, aggregation and grouping

_________________
Gonzalo Díaz


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 07, 2008 1:11 pm 
Newbie

Joined: Thu Feb 07, 2008 12:55 pm
Posts: 2
Thanks, but do you know any way using HQL queries?

That's just a small part in a larger query where it's easier to use HQL to pull exactly what I need.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 08, 2008 11:47 am 
Expert
Expert

Joined: Mon Nov 26, 2007 2:29 pm
Posts: 443
In principle, Hibernate supports both simple case ... when constructs and subqueries (if the underlying database supports them). So your query as it is should work.

My advice is, try to simplify the query by creating a view with the case...when part, and o from there. In that way, you will know if the problem is on that nested group specifically, or is part of the larger query to which you say it belongs.

_________________
Gonzalo Díaz


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