Skip to content
Advertisement

Calculating win streak from list of fixtures

I have a table fixtures with a list of matches played between two players in each, identified by their ID. I’m trying to use the table to calculate rolling winning/losing streaks for each player, but I’ve been having issues with my query below (see more on SQL Fiddle):

Below is the desired result, with records partitioned by player_id and ordered by edition_id. seq restarts the count every time res changes from a win to a loss or vice versa, and continues otherwise:

How should I fix my query to achieve this?

Advertisement

Answer

This requires a stock gaps and islands solution. Start by numbering each partition and subtract numbering partitioned by each result to identify groups of repeating sequences.

You can then use this grouping to further partition by to generate the required sequence:

See updated Fiddle

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement