I have a table named Game which is filled with individual players statlines from all their games in an nhl season. The fields That I am concerned with here are games_played which = n for the nth game of a player in the season. and name which is the players name.
Essentially I am trying to figure out how to grab the nth last game for each player.
For instance I know the following query can get me the last game for each player
Game.group(:name).having('games_played = MAX(games_played)')
However when I try the following (lets say n is 10)
Game.group(:name).having('games_played=MAX(games_played)-10')
I get no results
and in fact if I explicitly do something like
Game.group(:name).having('games_played=16')
I only get the last game of players who played exactly 16 games, instead of the 16th game for all players. (Which I guess explains the no results of the previous query)
How do I go about getting the nth last game for each player?
Is this even the right way to query this? I also have a player table where a player has_many games, and a game belongs_to a player. Should I be taking advantage of that?