Is there a way I can query the database to return only the elements that have happened successively (in time) according to a set variable?
Specifically, I need to find out how many times the user has won a game in a row. The games are stored in the database with an attribute win_loss (1 is win, 0 is loss).
I would like to see if the user has won 3 games in a row. Is there a way to do this in the database?
If not, what would it look like in the application?
I apologize ahead of time if this is confusing. Please ask questions and I will try to clear it up. I’m using Ruby on Rails.
I would take different approach. I would add a column consecutive_wins into your User model (negative numbers could represent consecutive losses if you need that as well).
EDIT: if you really need to query it…
Two queries:
SELECT max(created_at) AS newest, win_loss FROM games GROUP BY win_loss(I can’t recall how to do grouping functions in Rails right now but you should get the idea)created_at > newest_win AND created_at > newest_loseI recommend adding the column to user model, it will be better when you need to display the whole table of gamers and their consecutive wins/loses.