I’ve been puzzling over how to structure a special query, and the best I can figure out is that it would have to be some kind of complex subquery system.
I’m not entirely sure, though.
I have three tables, teams, matches, and scored.
The teams table contains a list of team numbers and corresponding team names.
The matches table contains a list recorded match results (one row per team per match), with the corresponding team number.
The scored table contains a list of all of the information about each score the team made, and the corresponding match result id.
┌────────────┐
| TEAMS |
├────────────┤
│ teamnumber |
│ teamname |
└────────────┘
┌───────────────┐
| MATCHES |
├───────────────┤
│ teamnumber |
│ matchresultid |
└───────────────┘
┌───────────────┐
| SCORED |
├───────────────┤
│ matchscoredid |
│ matchresultid |
└───────────────┘
Given a teamnumber, I need to get the average number of rows in scored per matchresultid. How would I do that?
I think the one element you left out that may have confused others is the actual score of the game a given team was playing against. So basically you want one team, and through the course of the season, you want the average # points they scored…
If you want a comparison of ALL teams averages, ignore the WHERE clause and just do a group by…
If the scored table has multiple rows per game, then a pre-aggregate would need to be done per game to have total points first, THEN get the average… something like
In THIS version of the query, if you wanted to compare all teams, remove the inner WHERE clause for a specific team, and apply the group by to the OUTER query by teamnumber and teamname.
To get your extra qualifier for the entire list, I’ve changed to LEFT joins, and finished with an IFNULL() for the average…