-->
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.  [ 4 posts ] 
Author Message
 Post subject: Problem with query using GROUP BY and ORDER BY
PostPosted: Fri Mar 17, 2006 11:43 am 
Newbie

Joined: Fri Mar 17, 2006 11:27 am
Posts: 2
I am using Hibernate 3.1 with MySQL and Java and I am trying to make a simple query to get the rows from table that appear more times in that table.

For example, suppose I have a table 'Sales' that stores the sales made by employees to customers. This table has two columns:

int customerId
int employeeId

Each row stores which employee sold something to which customer.
I want to retrieve the id of the employees with more sales, the number of sales of each employee, ordered by that number. In normal SQL I would do:

"select employeeId, count(employeeId) as X from Sales group by employeeId order by X"

That works perfectly in MySQL, the problem is that Hibernate (as far as I understand from the documentation) doesn't allow to usa a variable name for a column, or, in this case, use X for 'count(employeeId)'. I could change the query to:

"select employeeId, count(employeeId) from Sales group by employeeId order by count(employeeId)"

which is allowed by Hibernate, but not by MySQL, which would say: "Invalid use of group function"

I've been trying to solve it by using a SQL native query so I can use the first query I wrote, but it doesn't seem to work.

Anybody knows how to solve this?

Thanks!


Top
 Profile  
 
 Post subject: sql
PostPosted: Fri Mar 17, 2006 4:43 pm 
Expert
Expert

Joined: Fri Jul 22, 2005 2:42 pm
Posts: 670
Location: Seattle, WA
SQL for MySQL
select employeeId, count(employeeId) as c from Sales group by employeeId order by c

_________________
--------------
Konstantin

SourceLabs - dependable OpenSource systems


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 20, 2006 10:15 am 
Newbie

Joined: Fri Mar 17, 2006 11:27 am
Posts: 2
Yes, I know that works if I write it directly in MySQL, but Hibernate won't let me use it like that.

I looked at the query generated by Hibernate and noticed that it names the columns always in the same way, for ex:

the query
select employeeId, count(employeeId) from Sales

would be traduced to something like:

select Sales.employeeId as col_0_0_, count(employeeId) as col_1_0 from Sales

So I wrote the name col_1_0 in the Hibernate query to get:

select employeeId, count(employeeId) from Sales group by employeeId order by col_1_0

It is kind of ugly, but it works.


Top
 Profile  
 
 Post subject: 2c
PostPosted: Mon Mar 20, 2006 11:42 am 
Expert
Expert

Joined: Fri Jul 22, 2005 2:42 pm
Posts: 670
Location: Seattle, WA
IMO there is no need for HQL in this particular case. SQL (perhaps named query) seems more suitable.

_________________
--------------
Konstantin

SourceLabs - dependable OpenSource systems


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