-->
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: Nested select query to the same table with Group By
PostPosted: Thu Mar 29, 2012 1:51 pm 
Newbie

Joined: Thu Mar 29, 2012 11:48 am
Posts: 2
hi all,

I have a situation where I need to use an inner join on the same table with group by:
A, B (columns at table MY_TABLE, plus another PK column) and their values:
a, 1
a, 2
a, 3
b, 1
b, 2
b, 3
b, 4

Query:
SELECT a.A,B FROM Table_1 a
INNER JOIN (
select A, MAX(B) maxb
from Table_1
group by [A]
) b ON a.A=b.A AND a.B = b.maxb

That works great and returns what I need, which is the maximum of B for each A column:
A,B
a,3
b,4

How can I achieve this in hibernate?

When using the exact query above with EntityManager.createNativeQuery(String), I get the following exception:
org.hibernate.MappingException: No Dialect mapping for JDBC type: -9

When using that query with EntityManager.createQuery(String) - only replacing name of table with name of class - I get the following exception:
org.hibernate.hql.ast.QuerySyntaxException: unexpected token: ( near line 1, column 61 [SELECT a.A,B FROM MyTable a INNER JOIN (select A, MAX(B) maxb from MyTable group by A) b ON a.A=b.A AND a.B = b.maxb]

What am I doing wrong?

thanks for any thought
ShlomiJ


Top
 Profile  
 
 Post subject: Re: Nested select query to the same table with Group By
PostPosted: Sun Apr 01, 2012 3:39 am 
Newbie

Joined: Thu Mar 29, 2012 11:48 am
Posts: 2
Any thoughts?

SJ


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.