Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6243919
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T12:13:49+00:00 2026-05-24T12:13:49+00:00

I am using This JPA-Query: SELECT DISTINCT e.label FROM Entity e GROUP BY e.label

  • 0

I am using This JPA-Query:

SELECT DISTINCT e.label FROM Entity e 
GROUP BY e.label 
ORDER BY COUNT(e.label) DESC

I get no errors and the results are sorted almost correct but there are some values wrong (either two values are flipped or some single values are completly misplaced)

EDIT:

Adding COUNT(e.label) to my SELECT clause resolves this problem for this query.

But in a similar query which also contains a WHERE clause the problem persists:

SELECT DISTINCT e.label, COUNT(e.label) FROM Entity e 
WHERE TYPE(e.cat) = :category 
GROUP BY e.label 
ORDER BY COUNT(e.label) DESC
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T12:13:49+00:00Added an answer on May 24, 2026 at 12:13 pm

    You might need to include the COUNT(e.label) in your SELECT clause:

    SELECT DISTINCT e.label, COUNT(e.label) 
    FROM Entity e 
    GROUP BY e.label 
    ORDER BY COUNT(e.label) DESC
    

    UPDATE: Regarding the second query please read section 8.6. Polymorphic queries of the EntityManager documentation. It seems that if you make your queries in a way that requires multiple SELECTs, then the ORDER BY won’t work anymore. Using the TYPE keyword seems to be such a case. A quote from the above link:


    The following query would return all persistent objects:

    from java.lang.Object o // HQL only
    

    The interface Named might be implemented by various persistent classes:

    from Named n, Named m where n.name = m.name // HQL only
    

    Note that these last two queries will require more than one SQL SELECT. This means that the order by clause does not correctly order the whole result set. (It also means you can’t call these queries using Query.scroll().)


    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using this JPA-Query (EclipseLink): SELECT e FROM Entity e GROUP BY e.label
Whats wrong with the following JPA query select count(obj) from User as obj join
I have entity model like this (using EclipseLink and JPA 2.0): @Entity class A
I'm using Hibernate/JPA and have an @Entity object called Order, pointing at a MySQL
In my application am using JPA entity manager to persist data/fetch data. em.executeQuery(select *
So I have this SQL query, <named-query name=NQ::job_exists> <query> select 0 from dual where
Using this jquery code: <a href=javascript:void(0) id=m1>Get Selected id's</a> jQuery(#m1).click( function() { var s;
I have a problem getting this JPA query to work on MS SQL Server
I'm using JPA 2.0, more precisely Eclipselink. Here's my problem: I have an entity
I need to change the value of a column using JPA query syntax(JPQL), how

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.