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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:14:59+00:00 2026-05-26T01:14:59+00:00

Here are examples of the 3 tables I’m working with. Teams +—-+——+ | id

  • 0

Here are examples of the 3 tables I’m working with.


    Teams
    +----+------+
    | id | name |
    +----+------+
    |  1 | abc  |
    |  2 | def  |
    |  3 | ghi  |
    +----+------+


    Members
    +----+-----------+----------+---------+
    | id | firstname | lastname | team_id |
    +----+-----------+----------+---------+
    |  1 | joe       | smith    |       1 |
    |  2 | jared     | robinson |       1 |
    |  3 | sarah     | cole     |       3 |
    |  4 | jaci      | meyers   |       2 |
    +----+-----------+----------+---------+


    Goals
    +----+-----------+
    | id | member_id |
    +----+-----------+
    |  1 |         3 |
    |  2 |         2 |
    |  3 |         2 |
    |  4 |         3 |
    |  5 |         1 |
    +----+-----------+
    

And I’m trying to get a query that outputs something like this …


    Output
    +--------+----------------+-------------+
    | t.name | Count(members) | Count(goals)|
    +--------+----------------+-------------+
    | abc    |              2 |           3 |
    | def    |              1 |           2 |
    | ghi    |              1 |           0 |
    +--------+----------------+-------------+
    

This is the closest I’ve come, but when I use the group by in the subquery I get “Subquery returns more than 1 row”.

select t.name, count(*), 
    (select count(*)
    from teams t 
    inner join members m on m.team_id = t.id
    group by t.id)
from teams t 
inner join members m on m.team_id = t.id 
inner join goals g on g.member_id = m.id
group by t.id
  • 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-26T01:14:59+00:00Added an answer on May 26, 2026 at 1:14 am

    Based on my understanding, here is the query that I come up with:

    
        SELECT name, membersCount, IFNULL(totalCount, 0) goalsCount FROM
        (
          SELECT m.team_id, SUM(innerQuery.goalsCount) totalCount
          FROM (
            SELECT m.id memberId, COUNT(*) goalsCount
            FROM Members m
            JOIN Goals g
            ON m.id = g.member_id
            GROUP BY member_id
          ) innerQuery
          JOIN Members m
          ON innerQuery.memberId = m.id
          GROUP BY m.team_id
        ) inner_1
        RIGHT JOIN 
        (
          SELECT t.id, t.name, COUNT(*) membersCount
          FROM Teams t
          JOIN Members m
          ON t.id = m.team_id
          GROUP BY team_id
        ) inner_2
        ON inner_1.team_id = inner_2.id
    
    

    The breakdown of the query:

    #1. Get the member ID with its associated goals count (innerQuery)

    
    SELECT m.id memberId, COUNT(*) goalsCount
        FROM Members m
        JOIN Goals g
        ON m.id = g.member_id
        GROUP BY member_id
    

    #2. Get the team id for with the total SUM of the goals (inner_1)

    
         SELECT m.team_id, SUM(innerQuery.goalsCount) totalCount
          FROM (
              .... Sub-query in step 1
          ) innerQuery
          JOIN Members m
          ON innerQuery.memberId = m.id
          GROUP BY m.team_id
    

    #3. Get total members count per team (inner_2)

    
        SELECT t.id, t.name, COUNT(*) membersCount
          FROM Teams t
          JOIN Members m
          ON t.id = m.team_id
          GROUP BY team_id
    

    #4. RIGHT JOIN inner_1 and inner_2 (since there will be NULL) and use IFNULL to check and replace that 0

    
        SELECT name, membersCount, IFNULL(totalCount, 0) goalsCount FROM
        (
         .... Sub-query in step 2
        ) inner_1
        RIGHT JOIN 
        (
          .... Sub-query in step 3
        ) inner_2
        ON inner_1.team_id = inner_2.id
    
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using jquery data tables server side method example here http://www.datatables.net/examples/server_side/server_side.html My JSON
Here are two examples. One with required result, using tables: http://jsfiddle.net/Dhn3B/ Another with divs:
Rails noob here. I have a rails application with (in this example) three tables.
I tried one of the examples here to load the content in the div,
I read the MSDN documentation and examples here and I know that the correct
Using the client and server examples found here: http://www.winsocketdotnetworkprogramming.com/winsock2programming/winsock2advancedmailslot14.html Compiling them with VS2008, running
Im trying to hammer togehter a WYSIWYG-edit in c# following some examples from here
I'm trying to learn XPath. I looked at the other contains() examples around here,
Here's my situation.. Suppose you have the following model entities, which represent single tables
Here is the scenario I have: 5 tables. Author, Publisher, Region, Medium & Title

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.