-->
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.  [ 9 posts ] 
Author Message
 Post subject: How do I select based on what a String starts with?
PostPosted: Thu Jul 07, 2005 2:27 pm 
Newbie

Joined: Thu Jul 07, 2005 2:14 pm
Posts: 6
Hi
I need to write an HQL query that selects an attribute based on the first letter of the name.
For example, if I have an object w/ name and weight, I need to sum all the weights for all objects starting w/ A, then B, etc etc.
What is the code for this in HQL? I'm pretty sure I ahve to use REGEXP, but that's all I know.. I'm very new to HQL and don't really know SQL either.
Thanks for the help
Craig


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 07, 2005 2:41 pm 
Beginner
Beginner

Joined: Wed Jul 06, 2005 8:18 pm
Posts: 23
I don't think you can do this directly in an efficient manner but I'm not a SQL guru so take this with a grain of salt. What I would do is have another column that contains the first lettter of the name, then use it to group your result:

select cat.short_name, sum(cat.weight)
from Cat cat
group by cat.short_name

(where short_name is the new column)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 07, 2005 2:45 pm 
Newbie

Joined: Thu Jul 07, 2005 2:14 pm
Posts: 6
Well that's pretty much exactly what I want to do...but I can't actually touch the database to make a new column so that's not going to work.
But ya, if anyone knows how to write this code..

select name.firstLetter, sum(weight)

that would be a HUGE help

thanks
Craig


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 07, 2005 3:57 pm 
Expert
Expert

Joined: Thu Dec 04, 2003 12:36 pm
Posts: 275
Location: Bielefeld, Germany
First, I think you should take the time to read a SQL tutorial or take a course. I think that's worth it. Following you should read the Hibernate documentation (again). I don't think you will understand all of it without any SQL knowledge.

Well, in SQL (e.g. for MySQL) your query would look like this:
Code:
SELECT   SUM(weight)
FROM     tablename
GROUP BY SUBSTR(name, 1, 1)


In HQL you might test something like this (haven't tried):
Code:
select sum(c.weight) from Class c group by substring(c.name, 1, 1)


Good luck and best regards
Sven


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 07, 2005 4:00 pm 
Expert
Expert

Joined: Thu Dec 04, 2003 12:36 pm
Posts: 275
Location: Bielefeld, Germany
If you also need the first letter of the name, you will need something like this.

SQL:
Code:
SELECT   SUBSTR(name, 1, 1), SUM(weight)
FROM     tablename
GROUP BY SUBSTR(name, 1, 1)


HQL:
Code:
select substring(c.name, 1, 1), sum(c.weight) from Class c group by substring(c.name, 1, 1)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 07, 2005 4:05 pm 
Beginner
Beginner

Joined: Wed Jul 06, 2005 8:18 pm
Posts: 23
I do need to take a SQL course as well since I didn't know I can do group by on computed values :(


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 07, 2005 4:07 pm 
Newbie

Joined: Thu Jul 07, 2005 2:14 pm
Posts: 6
Thanks a lot, I actually just found that substring function in an oracle book, but i was trying to use SUBSTR instead of substring. And ya, I'll take a course when I can, but I need my knowledge NOW haha.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 07, 2005 4:09 pm 
Expert
Expert

Joined: Thu Dec 04, 2003 12:36 pm
Posts: 275
Location: Bielefeld, Germany
You can in SQL, but I don't know if it's possible with HQL as well. Couldn't find it in the documentation, but I think it is possible.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 07, 2005 5:18 pm 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
ctran wrote:
I don't think you can do this directly in an efficient manner but I'm not a SQL guru so take this with a grain of salt. What I would do is have another column that contains the first lettter of the name, then use it to group your result:

select cat.short_name, sum(cat.weight)
from Cat cat
group by cat.short_name

(where short_name is the new column)


Read up on formula in the reference doc. I think it might provide what you're looking for without changing the database.


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