• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Group by desc in jpql

 
Sharath Upadhya
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am having issues in using the 'group by' clause in JPQL.

I need to fetch all the records grouped by their parent records, but need to group by the latest record.(grouping in reverse order)

I tried running 'select * from table_name group by parent_record desc' on mysql and there was no problem.

but when I tried it with JPQL, it started giving exceptions.

please guys, help me if anyone of you have tried this....

thanks in advance.
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is your jpql query?
 
Sharath Upadhya
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for responding. here's my JPQL query.

SELECT x FROM Discussions AS x WHERE x.id IN (SELECT y.id FROM Discussions AS y WHERE y.parentDiscussion IS NOT NULL GROUP BY y.parentDiscussion DESC);

This is just a simplified query. if I remove 'DESC', i am getting the output. but not the expected one.

without DESC, this query fetches only the first comment for any discussion. my requirement is to fetch only the latest comments for all the discussions.
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you using a sub-query in your jpql query while you're not using a sub-query in your native query?
 
Sharath Upadhya
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using multiple sub-queries in both queries.

but I haven't posted those queries just for simplicity. and to give you an idea of what am trying to accomplish.

and it cannot be done without sub-queries as you know it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic