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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:00:29+00:00 2026-05-22T16:00:29+00:00

I have an application that uses a nested set model class to organise my

  • 0

I have an application that uses a nested set model class to organise my data, however I’m trying to write a query that will count the total amount of people in each group.

table: person_to_group

 ----+------------+-----------
|ID  | Person_ID  | Group_ID  |
 ----+------------+-----------
| 1  | 3          | 1         |
| 2  | 3          | 2         |
| 3  | 5          | 2         |
| 4  | 7          | 3         |
 ----+------------+-----------

table: groups

 ----------+--------------+--------------+-------------
|Group_ID  | Group_Name   | Group_Left   | Group_Right |
 ----------+--------------+--------------+-------------
| 1        | Root         | 1            | 6           |
| 2        | Node         | 2            | 5           |
| 3        | Sub Node     | 3            | 4           |
 ----------+--------------+--------------+-------------

My query will be run within a while loop which lists all the group names.

I’m trying to accomplish a result like this:

Root – Members (3) <——- Notice that I want subgroups to be included in the count, and I don’t want members to be counted more than once.

Any help would be much appreciated!

  • 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-22T16:00:30+00:00Added an answer on May 22, 2026 at 4:00 pm

    Since you run it inside a while loop with group names you can probably get the group’s left and right values instead and put them into this query:

    SELECT count(*) AS members FROM 
    ( SELECT DISTINCT ptg.person_ID FROM groups g 
    JOIN person_to_group ptg
    ON ptg.Group_ID=g.Group_ID
    WHERE g.Group_Left  >= 1 
    AND   g.Group_Right <= 6 ) m
    

    This works for a given group. If you wanted to get a full list of groups with member count for each in one query you would have to use something like:

    SELECT m.Group_Name, count(*) AS members FROM
      ( SELECT g.Group_ID, g.Group_Name, ptg.Person_ID 
        FROM groups g 
        JOIN groups gsub 
          ON gsub.Group_Left >= g.Group_Left AND gsub.Group_Right <= g.Group_Right
        JOIN person_to_group ptg
          ON gsub.Group_ID = ptg.Group_ID
        GROUP BY g.Group_ID, ptg.Person_ID ) m
    GROUP BY m.Group_ID
    

    However I think the first solution with a loop outside of sql would be more efficient.

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

Sidebar

Related Questions

I have client application that uses WCF service to insert some data to backend
I have an application that uses the accelerometer. Sometimes, the application will launch without
I have an application that uses a cron like job to update a set
We have an application that uses a dual monitor setup - User A will
I have an application that uses a UITableView to present data that I have
I have a console application that uses an app.config file however I can't figure
I have an application in java that uses two different threads.And for sharing data
We have an ASP.NET 2.0 application that uses Enterprise Library Data Access Application Block
I have an application that uses NHibernate as its ORM and sometimes it experiences
I have an application that uses window.open() to generate dynamic popups. Unfortunately, I've had

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.