A continuation from this question I need a SQL statement that returns the number rows in a table with a specific value.
We left off with a SQL statement as follows...
SELECT t.teamid,
t.teamname,
COALESCE(COUNT(p.playerid), 0) AS playercount,
t.rosterspots
FROM TEAMS t
LEFT JOIN PLAYERS p ON p.teamid = t.teamid
GROUP BY t.teamid, t.teamname, t.rosterspots
I have one more constraint to add. WHAT IF, players need to pass a Medical Exam before they count towards the "playercount"?
I'll introduce the table.
MedicalTests PlayerId PassedMedical 1 1 2 0 3 1 4 1
Where "PassedMedical" is a bit (1 = true).
And also add 1 more ROW of data to the Teams table.
TeamId Team Name 3 Toronto Rapters
This way I have a team with 0 players.
And the expected OUTPUT changes to:
Team Name PlayerCount Miami Heat 2 New York Knicks 1 Toronto Rapters 0
since one of the Miami Heat players has not yet passed the medical.
If I add
LEFT JOIN MEDICALTESTS m ON p.PlayerId = m.PlayerId
WHERE m.PassedMedical = 1
To the above statement I lose all the "0" rows?
Thanks,
Justin