I’m trying to analyze some baseball stats and I’m having some trouble achieving what seems like it should be a simple task. Take a look at the following result set:
GAME_PK REC_SEQ BatterId PlayNumber EventType
287576 6 462101 1 single
287576 14 519048 2 single
287576 25 435079 3 strikeout
287576 26 435079 4 stolen_base_home
287576 28 435079 5 stolen_base_2b
The PlayNumber column is being generated by me using ROW_NUMBER() OVER (ORDER BY GAME_PK, REC_SEQ). The rest comes directly from an MLB stats database. REC_SEQ is the sequence number of the event within the game. EventType is essentially the result of an at-bat.
I would like PlayNumber to increment only when the BatterId changes. But it must respect the ordering of REC_SEQ. So I don’t think I can use RANK or DENSE_RANK, but those seem to be very close to what I need.
I would like my result set to look like this:
GAME_PK REC_SEQ BatterId PlayNumber EventType
287576 6 462101 1 single
287576 14 519048 2 single
287576 25 435079 3 strikeout
287576 26 435079 3 stolen_base_home
287576 28 435079 3 stolen_base_2b
Any help is appreciated.
Thanks!
EDIT: A batter can appear more than once during a game. He should be assigned a new PlayNumber for each appearance. Basically, each new at-bat requires a new PlayNumber.
Edit: It seems like the only way this can be accomplished is to figure out where each group begins and ends by determining which sequential records share a batterId. This is done by joining the records with themselves offset by 1 rownum to determine where each group begins. Once we have a collection of the starts of each group (
GroupSets), we can determine to which group each individual record belongs to produce the correct numbering:Demo: http://www.sqlfiddle.com/#!3/a5e68/50
old code that doesn’t handle interleaving:
Actually the
DENSE_RANK()function should do it. However, we need to rank over the values of theMIN(REC_SEQ)per BatterId group in order to useREC_SEQto control the order. Something like this should do it:Demo: http://www.sqlfiddle.com/#!3/0682e/4