• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how can I get every first result in each group by hibernate Criteria?

 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
how can I get every first result in each group by hibernate Criteria?

for example, there're 4 records in database
id name value group
1 a1 1 a
2 a2 2 a
3 b1 1 b
4 b2 2 b

what i wanna get is [a1, b1].
how to determine it's the first record in the group?
if its value is smallest, it's the first value in the group.

 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And what's the expected output if you have a 5th record like:

id name value group
5 a3 1 a
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the ORDER BY which defines what row is 'first'?
 
Greenhorn
Posts: 3
Oracle Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


You could do this with a self-join if you prefer.
 
drac yang
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

K. Holgate wrote:

You could do this with a self-join if you prefer.


thanks, how can I fulfill this by hibernate criteria?
 
drac yang
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Roel De Nijs wrote:And what's the expected output if you have a 5th record like:

id name value group
5 a3 1 a


yes, you are right, my provided conditions are insufficient, we can use order by.
key point here is how to fulfill this by hibernate criteria?
 
drac yang
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dave Tolls wrote:What is the ORDER BY which defines what row is 'first'?


ORDER BY value, if only we can get the first result in the group.
and the most important thing i.e. my original question is how to fulfill this requirement by hibernate criteria.
 
Dave Tolls
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Using a criteria may be the key point, but until you can figure out the SQL you would right outside of a Criteria you won't get very far.

So (assuming K. Holgate's query is correct, which is something you'd need to test) you would be looking at a Subquery method, probably the eq() one.
 
drac yang
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dave Tolls wrote:Using a criteria may be the key point, but until you can figure out the SQL you would right outside of a Criteria you won't get very far.

So (assuming K. Holgate's query is correct, which is something you'd need to test) you would be looking at a Subquery method, probably the eq() one.


thank you, i'll have to check more related criteria materials.
 
Thank you my well lotioned goddess! Here, have my favorite tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic