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 6086829
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:48:02+00:00 2026-05-23T11:48:02+00:00

In a custom blog platform with User(s), Post(s), and Message(s). I know how to

  • 0

In a custom blog platform with User(s), Post(s), and Message(s).
I know how to do an HQL query with distinct Users and their Post count. Also distinct Users and their Message count.

But if I combine the two with “inner join” technique I get the same count for Posts and Messages. I understand why this is happening because of the joins. How could I do sub-selects in the HQL to get the two counts as separately but as one trip to the database?

Here is an example of the last HQL query I tried.

   select u.username, count(m), count(p) from User as u 
   inner join u.Messages as m
   inner join u.Posts as p
   group by u.id 
   order by count(m) desc

Note: I will be changing the order by based off of an option on a web page.

  • 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-23T11:48:03+00:00Added an answer on May 23, 2026 at 11:48 am
    select u.username,
           (select count(m.id) from Message m where m.user = u) as messageCount,
           (select count(p.id) from Post p where p.user = u) as postCount
    from User u
    order by messageCount desc
    

    You might have issues with the order by due to the bug http://opensource.atlassian.com/projects/hibernate/browse/HHH-892 which is supposed to be fixed, but doesn’t seem to be in my version of Hibernate. If it’s the case, replace order by messageCount by order by 2.

    But, as Michael J. Lee indicates, you may also use a simple join and distinct count, which should be more efficient :

    select u.username, count(distinct m.id), count(distinct p.id) from User u 
    inner join u.messages m
    inner join u.posts p
    group by u.username)
    order by (count(distinct m.id) desc
    

    Using left joins instead of inner joins will also let you get users without any post or without any message.

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

Sidebar

Related Questions

I'm developing a custom wordpress blog theme. Does anyone know if there is a
Help, I'm trying to create a new post in my wordpress blog with custom
Using Marius Gedminas's excellent blog post , I have created a custom traverser for
I followed a blog post here to use a custom validator to validate a
in the following post: http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-4-custom-object-templates.html <%= Html.Editor(prop.PropertyName) %> Html.Editor creates a text box with
http://weddings.agapewith.us/photos/caleb-promise Notice the page appears under blog. They are a custom post-type of photos
I'm working on a custom data type for Umbraco based on Tim's Blog Post:
I created a custom annotation for logging following the example in this blog post
I am using the custom post types in my wordpress blog and want to
I have a wordpress blog. I made a custom page-template that allows users to

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.