-->
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: HQL for retrieving an object with a max clause
PostPosted: Thu Jul 23, 2009 1:37 pm 
Newbie

Joined: Thu Jul 23, 2009 1:31 pm
Posts: 1
I have a table full of addresses. In this table, multiple copies of the same address or stored, but they become deprecated or considered old addresses. So what I need is the current address, and the current address is the one that has the highest ID [max(id)]. I'm trying to retrieve the object but I can't get the clause correct; something like:
select add from addresses as ad where state = 'tx' and max(add.id)
I realize that syntactically that is incorrect, but I've tried several approaches and haven't gotten it. I have no control over the data model, and I'd prefer not to join to myself (unless that's the only way).
TIA


Top
 Profile  
 
 Post subject: Re: HQL for retrieving an object with a max clause
PostPosted: Wed Aug 12, 2009 10:06 am 
Newbie

Joined: Wed Aug 12, 2009 10:00 am
Posts: 1
give this query a try:
Code:
from address add where add.state = 'tx' and add.id >= (select max(maxadd.id) from address maxadd where maxadd.state = add.state)

You should also try to determine what makes an address a duplicate of another one because now the only thing that is common between the query and the subselect is the 'state' field

hope this helps

Koen Weyn


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.