I have the following data sorted by player_id and match_date. I would like to know a group of records that has the maximum number of consecutive runs (4 runs from 2014-04-03 to 2014-04-12 for 3 consecutive times)
player_id match_date runs
1 2014-04-01 5
1 2014-04-02 55
1 2014-04-03 4
1 2014-04-10 4
1 2014-04-12 4
1 2014-04-14 3
1 2014-04-19 4
1 2014-04-20 44
2 2014-04-01 23
2 2014-04-02 23
2 2014-04-03 23
2 2014-04-10 23
2 2014-04-12 4
2 2014-04-14 3
2 2014-04-19 23
2 2014-04-20 1
I came up with the following SQL:
select *,row_number() over (partition by ranked.player_id,ranked.runs
order by ranked.match_date) as R from (
select player_id ,match_date,runs from players order by 1,2 desc )
ranked order by ranked.player_id, match_date asc
But this one continues the rating from previous consecutive runs (4 runs on 2014-04-19 for player 1 are expected to get rank 1, but get rank 4, since there were 3 occurrences of the same section already). Similarly, 23 runs for player 2 on 2014-04-19 are expected to receive 1st level, but will receive 5th rank, as this player already had 4 cases out of 23 runs.
reset 1, ?
, , SQL SQLFiddle.