Question

Sorry for the vague question, but I can only explain my question further by giving an example, of what I need the solution for. In basketball, my friends and I take 20 shots at a time and record how many went in - I want to make a database to store each of these n/20 sets for each player in order to get averages. MySQL doesn't sound like the best solution as it can't store simple arrays. Maybe I could store this data as a 2D array? eg.

stats = [ 
          [ ["me" , 12 , 14 , 8 , 19 , 13 , 10...] ],
          [ ["friend1" , 11 , 9 , 14 , 15 , 16 , 18...] ],
          [ ["friend2" , 17 , 9 , 18 , 14 , 17 , 17...] ]
        ]

I would like to keep each set of 20 as opposed to a running total percentage - how should I go about this? Thanks.

Was it helpful?

Solution

I would store it in separate tables, so you got:

Games - GameID, Date, NumberOfShots (so you're not fixed to 20), Etc
Players - PlayerID, Name, etc
Turns - TurnID, GameID, PlayerID, Score

From these you can easily aggregate per player, something like this.

Get avarage percentage per player over the last 30 days.

select
  p.PlayerID,
  p.Name
  AVG(p.Score / g.NumberOfShots) * 100 as Percentage
from
  Games g
  inner join Players p on p.GameID = g.GameID
  inner join Turns t on t.GameID = g.GameID and t.PlayerID = p.PlayerID
where
  g.Date > (sysdate() - 30)
group by 
  p.PlayerID,
  p.Name
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top