SQL Using Query Results to Create Another Table

(SELECT childinfo.first,childinfo.last,COUNT(clubinfo.club) AS clubs_per_student FROM clubinfo
LEFT JOIN childinfo
ON childinfo.child_id=clubinfo.child_id
GROUP BY concat(studentinfo.first,' ',studentinfo.last)
)

      

Above is a query I wrote to combine the childinfo tables (containing the child_id, first and last name columns) and clubinfo (containing the child_id and club columns). The above query will create a table that looks something like this:

first     |     last     |     sports_per_child
Sally     |     Jones    |            2
Phil      |     Jones    |            1
Jane      |     Doe      |            1
John      |     Doe      |            1

      

What I want to do is use the results of this query to compile a report on the total number of students that are in a given number of clubs. So, for example, in the above table, a report will be prepared that there are 3 students in 1 club and 1 student who is in 2 clubs.

Any ideas on how to write a query that uses my previously written query as a subquery to accomplish this?

+3


source to share


1 answer


You just need to get score and group by sports_per_child:



SELECT sports_per_child, count(*) FROM
(subquery) AS S
group by sports_per_child

      

+1


source







All Articles