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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:40:36+00:00 2026-05-26T05:40:36+00:00

I have three tables: User — contains users Group — contains a set of

  • 0

I have three tables:

User -- contains users
Group -- contains a set of groups that users can be members of
Membership -- Contains PKs from User and Group to indicate that User is a member of group

The PKs are UserId, GroupId, and MembershipId respectively, and Membership has FKs to the other two tables.

This is pretty standard stuff, the one tweak is that a user can be a member of multiple groups, and groups have a level (1, 2, 3, 4). The user’s current group is the group they are a member of that has the highest level.

Now I want to get a count of the number of members in each group. At the moment I have something like this:

SELECT Count(*) FROM Membership
    FROM Membership M1
    INNER JOIN Group G1 ON M1.GroupId = G1.GroupId
    INNER JOIN User ON User.UserId = M1.UserId
WHERE
    M1.GroupId = @groupId
    AND NOT EXISTS (
            SELECT * FROM Membership M2
            INNER JOIN Group G2 ON M2.GroupId = G2.GroupId
            WHERE M2.UserId = M1.UserId
            AND G1.Priority < G2.Priority)

So for each user I am looking to see if there is another membership in a group with a higher priority. This is obviously pretty horrible, especially when you have a lot of rows, but I am at a little bit of a loss on how to improve it.

Ideally, I’d like to transform it so that I get the counts for all Groups rather than just one select (as above.)

Any ideas how to do this more efficiently?

  • 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-26T05:40:37+00:00Added an answer on May 26, 2026 at 5:40 am

    Which RDBMS? this will work in SQL Server…

    SELECT
        HighestGroupId,
        COUNT(*) AS Members
    FROM
    (
        SELECT
            m.UserID,
            MAX(m.GroupId) AS HighestGroupId
        FROM Membership m
        GROUP BY m.UserID
    ) sub
    GROUP BY HighestGroupId
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have three tables: Users, Groups and GroupsUsers User table: id: 1 username: admin
Suppose I have three tables: user , group and xref , a table that
I have three tables: Project: ... relations: User: local: authorId foreign: id Users: class:
I have three tables to define users: USER: user_id (int), username (varchar) USER_METADATA_FIELD: user_metadata_field_id
I have three tables called: users , facilities , and staff_facilities . users contains
I have three tables, that are like: users --------------- id etc... badges --------------- id
I have three tables say 'user', 'user_resources' and 'desktop_resources'. 'user' contains - emp_id,name and
I have three tables, 1-Users, 2-Softwares, 3-UserSoftwares. if suppose, Users table having 6 user
For example, I have three tables that store different types of users with different
I have three tables that I am working with User, Application, ApplicationAdministrator. ApplicationAdministrator is

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.