-->
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: How to write a subquery in "order by"
PostPosted: Sun Jan 29, 2006 10:21 pm 
Newbie

Joined: Sun Jan 29, 2006 10:12 pm
Posts: 1
Hi, I am trying to write an order by with a subquery for a field to order on. Here was my first try (gen'd SQL shown at bottom):

Code:
select t from Task as t order by coalesce(completed, '3000-01-01') desc, started desc, maxelement(updates.entered) desc

I didn't totally expect it to work, but it was close. The only problem was that it used the 'id' field of the Update instead of 'entered'. So here is my second try:

Code:
select t from Task as t order by coalesce(completed, '3000-01-01') desc, started desc, (select max(u.entered) from Update as u where u.task = t) desc

But this gives an exception and I think it's because I am using the subquery. Here's the exception details:

Code:
org.hibernate.hql.ast.QuerySyntaxException: unexpected AST node: query [select t from taskqueue.model.Task as t order by coalesce(completed, '3000-01-01') desc, started desc, (select max(u.entered) from taskqueue.model.Update as u where u.task = t) desc]

Any ideas what the right HQL is or how I can get around this? Thanks.

Details:
Hibernate version: 3.1.1

Name and version of the database you are using: MySQL 4.1

The generated SQL (show_sql=true):

Code:
DEBUG (org.hibernate.SQL:346) - select task0_.id as id0_, task0_.name as name0_, task0_.description as descript3_0_, task0_.started as started0_, task0_.completed as completed0_ from tq_task task0_ order by coalesce(task0_.completed, '3000-01-01') desc, task0_.started desc, (select max(updates1_.id) from tq_update updates1_ where task0_.id=updates1_.task_id) desc


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.