-->
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.  [ 7 posts ] 
Author Message
 Post subject: What is the equivalent of an SQL join subselect?
PostPosted: Mon Mar 22, 2010 9:27 pm 
Newbie

Joined: Mon Mar 22, 2010 9:14 pm
Posts: 6
Hi, I am losing my mind with HQL as it seems to have a few limitations.
For example I have searched far and wide to find an answer to this problem but have failed miserably!

Say i have a simple table with table with 3 columns: Person(id,name,number)
Now i simply want to get all the information of the person with the longest number. So my HQL looks like this:

Code:
select pr.personid,pr.name, max(length(pr.number)) from Person pr


This will obviously get a group by error from my PostgreSQL database.

Now I can do this in SQL using a join subselect in the from clause but since i cant do that in hql, im lost. I feel like there is a simple solutin but i cant find it!
PLEEEASE HELP!


Top
 Profile  
 
 Post subject: Re: What is the equivalent of an SQL join subselect?
PostPosted: Tue Mar 23, 2010 6:13 am 
Expert
Expert

Joined: Thu Sep 22, 2005 10:29 am
Posts: 285
Location: Almassera/Valencia/Spain/EU/Earth/Solar system/Milky Way/Local Group/Virgo Supercluster
What about this, it is not join subselect but I think it gets the data you need
Code:
select
   pr.personid,
   pr.name,
   length(pr.number)
from
   Person pr
where
   length(pr.number)  = (
      select
         max(length(p.number))
      from
         Person p
   )

14.13. Subqueries


Top
 Profile  
 
 Post subject: Re: What is the equivalent of an SQL join subselect?
PostPosted: Mon Mar 29, 2010 2:30 am 
Newbie

Joined: Mon Mar 22, 2010 9:14 pm
Posts: 6
hehe thanks pepelnm, that was actually a simplified version of the statement i was actually using which had some joins, so i couldnt use your solution. but i changed my tables around so i was able to use hql to get the result i wanted, which kinda sux but anyways thanks again and if this same problem comes up again i would definately love to see your soluton for it...
cheears :)


Top
 Profile  
 
 Post subject: Re: What is the equivalent of an SQL join subselect?
PostPosted: Mon Mar 29, 2010 9:23 pm 
Newbie

Joined: Mon Mar 22, 2010 9:14 pm
Posts: 6
ok I've run into another problem that i could have solved using join subselects in from clause in sql but for the life of me i cant figure out how to do this in hql.
This is a much better representation of what i was trying to do earlier in the thread.

I have 2 tables linked by a many-many relationship through another table.
Table1:Callrate
Table2:Prefix
Table3:Callrateprefix

Now when i reverse engineer these three tables in hibernate it creates pojo etc for only callrate and prefix and a relationship Set for eachother. (i hope im making sense my hibernate/db lingo is very immature).

It's a quite simple relationship between callrate and prefix. Every prefix has many callrates. (and for a different purpose callrates can also have many prefixes but this isnt important in this case)

So what i am trying to do is get the cheapest rate for every prefix and some rate details also.

To get the cheapest rate for every prefix i use:
Code:
select pr2.prefixid, min(cr2.rate)
from Callrate cr2 join cr2.prefixes pr2
group by pr2.prefixid

But i cant get any other callrate info along with the result like callratename etc. because ill have to put it in the group by clause and that just screws up the result.

I tried and tried and researched and tried some more (and screamed WHYYY OH WHYYY are there no join subselects!) and the best thing i could come up with was:
Code:
select pr.prefixid, cr.callrateid,cr.name,(select min(cr2.rate)
from Callrate cr2 join cr2.prefixes pr2
where cr2=cr and pr2=pr
group by pr2.prefixid)
from Prefix pr join pr.callrates cr


Which works when theres no joins but for some reason just returns the full list of rateid/prefixid matchs and there rate. I would love to know how to solve this because it will solve a bunch of other problems like this that im having to do weird things to solve. Thanks in advance :)


Top
 Profile  
 
 Post subject: Re: What is the equivalent of an SQL join subselect?
PostPosted: Wed Mar 31, 2010 11:24 am 
Newbie

Joined: Mon Mar 22, 2010 9:14 pm
Posts: 6
i thought this would be a bit harder :s
anyone???


Top
 Profile  
 
 Post subject: Re: What is the equivalent of an SQL join subselect?
PostPosted: Mon Apr 05, 2010 8:01 pm 
Newbie

Joined: Mon Mar 22, 2010 9:14 pm
Posts: 6
This is reeally affecting my work, can anyone please help with this problem.
It would be very much appreciated!
Happy Easter!


Top
 Profile  
 
 Post subject: Re: What is the equivalent of an SQL join subselect?
PostPosted: Thu Apr 15, 2010 11:33 pm 
Newbie

Joined: Mon Mar 22, 2010 9:14 pm
Posts: 6
just checking if anyones replied... so im gona take the lack of response as a sign that it cant be done. thought post to let anyone who finds this thread to know that i still dont have an answer to this and doesnt look like i will. so sad hibernate. back to sql queries i guess.


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