If I have a SQL table that looks like this below how can I calculate the current winning or losing streak (and having the winning or losing streak grouped/reset by season). I want to update the table and fill in the streak for every record.
So for #1 the streak would be "-1", #2 would be "1", #3 would be "2" but once we got down to #7 it would be reset to "1" again. (+1 means "Won 1 game" and -1 means "Lost 1 game", etc.)
ID team date Result season streak
1 76ers 2000-01-01 Loss 2000 Null
2 76ers 2000-01-05 Win 2000 Null
3 76ers 2000-01-08 Win 2000 Null
4 Lakers 2000-01-03 Loss 2000 Null
5 Lakers 2000-01-07 Loss 2000 Null
6 Lakers 2000-01-01 Win 2000 Null
7 76ers 2002-03-01 Win 2001 Null
8 76ers 2002-03-05 Win 2001 Null
9 76ers 2002-03-08 Loss 2001 Null
10 Lakers 2002-03-03 Loss 2001 Null
11 Lakers 2002-03-07 Loss 2001 Null
12 Lakers 2002-03-01 Win 2001 Null
For each game, count games with the same result that came before it, such that there is no game with the opposite result in between. Store the results in a temporary table:
Then, update the original table (and negate the losing streaks in the process):
Finally, clean-up the temporary table: