I have a requirement to build a leaderboard of users of a fantasy football game. The simplified database of the game is as follows:
users
havesquad_players
insquads
for eachmatches
(related by transfer_period)players
havematch_points
for eachmatches
squad_players
have position, priority. The priority is the order of substitutions if asquad_players
does not appear in amatches
squads
haveformations
which determine the maximum number of players to select from of each position ordered by priority
The database is MySQL 5.6 and the maximum number of users
is 10K.
I am able to inner join (in order) squad_players
, squads
, matches
, match_points
to get the point of each squad_players
who played (players
who didn't play have no match_points
).
I am struggling to SUM the points of x squad_players
of each squads
where x is in formations
and determined by position of the squad_players.
I have tried to modify versions of Top n-rows with group by with correlated subqueries but without success.
What should my approach be here?
My Expected Output is something like this
|---------|-----------|------------|----------|
| User ID | SQUAD ID | POSITION | POINTS |
|---------|-----------|------------|----------|
| 12 | 34 | defender | 12 |
|---------|-----------|------------|----------|
| 12 | 34 | forward | 2 |
|---------|-----------|------------|----------|
| 117 | 31 | midfielder | 5 |
|---------|-----------|------------|----------|
Notes:
I must add the outputs may not reflect the problem statement.
If you look into the Sample Dataset Link, you will find a view named
normalized_squad_player_points
that is the denormalized table I want to work with to create different stats.
Final Output for the leaderboard will be something like this
|---------------------|------------------|
| User ID | Points |
|---------------------|------------------|
| 12 | 34 |
|---------------------|------------------|
| 56 | 32 |
|---------------------|------------------|
| 117 | 31 |
|---------------------|------------------|
Additional information
- SQL Fiddle link to schema.
- Link to exported Schema and Sample Dataset.
- I'm open to modifying table definitions if necessary.
- Pastebin link to
squads
andmatch_points
inserts.
CREATE TABLE
statements, sample data (INSERT a few rows) and wanted result set. Reading your question, it is not very clear which which names refer to tables and which to columns