In my database schema, I have clubs, teams and players and I want to create a report that counts the total number of teams in a club, as well as the number of teams that have no players. The output would be something like:
CLUB #TEAMS #EMPTYTEAMS
Cobras 5 0
Eagles 7 2
My schema is described in the following pseudo-code:
TABLE CLUB
ID int
NAME string
TABLE TEAM
ID int
NAME string
TABLE PLAYER
ID int
NAME string
TABLE CLUB_TEAM
ID int
CLUB_ID int -- foreign key into CLUB
TEAM_ID int -- foreign key into TEAM
TABLE MEMBERSHIP
ID int
PLAYER_ID int -- foreign key into PLAYER
TEAM_ID int -- foreign key into TEAM
Some sample data:
CLUB
0 Eagles
1 Cobras
TEAM
0 Eagles U10
1 Eagles U11
2 Eagles U12
...
7 Cobras U11
8 Cobras U12
9 Cobras U13
...
PLAYER
0 John Doe
1 Jane Doe
...
CLUB_TEAM
0 0 0 - The Eagles Club has an U10 team
1 0 1
2 0 2
...
7 1 7
8 1 8
9 1 9
MEMBERSHIP
0 0 7 - John plays for Cobras U11
1 1 9 - His older sister, Jane, plays for Cobras U13
I've managed to develop a query that will give me the number of teams in each club:
SELECT CLUB.ID, CLUB.NAME, COUNT(CLUB_TEAM.ID) AS "#TEAMS"
FROM CLUB
INNER JOIN CLUB_TEAM
ON CLUB_TEAM.CLUB_ID = CLUB.ID
INNER JOIN TEAM
ON CLUB_TEAM.TEAM_ID = TEAM.ID
GROUP BY CLUB.ID
However, I haven't been able to extend this query to include team membership data and count teams with no players. Any suggestions?