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.  [ 1 post ] 
Author Message
 Post subject: subqueries in SELECT or ORDER BY clauses
PostPosted: Wed Jan 31, 2007 8:16 pm 
Beginner
Beginner

Joined: Wed Jul 05, 2006 12:45 pm
Posts: 21
Are subqueries supported in HQL SELECT or ORDER BY clauses?

I have seen, and have successfully done, subqueries in the WHERE clause, such as this:

Code:
select ria.Employee

from EmployeeResearchItemAssociation as ria

where ria.ResearchItem = :researchItem
     and ria.IsActive = :isActive"
     and ria.Employee.Type != :employeeType
     and (ria.QueueLimit -
          (select count(rriqe.ID)
               from ResearchRequestItemQueueEntry as rriqe"
               where rriqe.User = ria.Employee and
                    rriqe.ResearchRequestItem.ResearchItem = ria.ResearchItem
                    and rriqe.DeletionDate = null
           ) > 0)


However, when I try to sort by that same calculation, I run into errors. I have tried putting the calculation directly into the ORDER BY clause, like this:

Code:
select ria.Employee

from EmployeeResearchItemAssociation as ria

where ria.ResearchItem = :researchItem
     and ria.IsActive = :isActive"
     and ria.Employee.Type != :employeeType
     and (ria.QueueLimit -
          (select count(rriqe.ID)
               from ResearchRequestItemQueueEntry as rriqe"
               where rriqe.User = ria.Employee and
                    rriqe.ResearchRequestItem.ResearchItem = ria.ResearchItem
                    and rriqe.DeletionDate = null
           ) > 0)

order by (select count(rriqe.ID)
      from ResearchRequestItemQueueEntry as rriqe
      where rriqe.User = ria.Employee and
           rriqe.ResearchRequestItem.ResearchItem = ria.ResearchItem
           and rriqe.DeletionDate = null
      )


...but this put a "half-interpreted" version of the HQL into the query sent to the db, and of course failed. The class names referenced in the ORDER BY became fully qualified, but other than that, nothing else was interpreted there.

I have also tried putting a subquery into the SELECT clause, giving it an alias, and referencing the alias in the ORDER BY:

Code:
select ria.Employee,
     (select count(rriqe.ID)
      from ResearchRequestItemQueueEntry as rriqe
      where rriqe.User = ria.Employee and
           rriqe.ResearchRequestItem.ResearchItem = ria.ResearchItem
           and rriqe.DeletionDate = null
      ) as theorder

from EmployeeResearchItemAssociation as ria

where ria.ResearchItem = :researchItem
     and ria.IsActive = :isActive"
     and ria.Employee.Type != :employeeType
     and (ria.QueueLimit -
          (select count(rriqe.ID)
               from ResearchRequestItemQueueEntry as rriqe"
               where rriqe.User = ria.Employee and
                    rriqe.ResearchRequestItem.ResearchItem = ria.ResearchItem
                    and rriqe.DeletionDate = null
           ) > 0)

order by theorder


...but this results in an error saying that I "aggregate function expected before (".

Can anyone shed some light on the right way to do this? Thanks very much!

Hibernate version: 1.2.0 beta3


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.