Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7815007
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T05:22:50+00:00 2026-06-02T05:22:50+00:00

I’m trying to analyze some baseball stats and I’m having some trouble achieving what

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T05:22:52+00:00Added an answer on June 2, 2026 at 5:22 am

    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:

    with GroupSets as (
    select
        row_number() over (order by s1.rec_seq) as rownum,
        s1.game_pk, s1.rec_seq, s1.batterid, s2.batterid as nextbatterid,
        s1.eventtype
    from (select *, row_number() over (order by rec_seq) as rownum from stats) s1
    left join (select rec_seq, batterid,
               row_number() over (order by rec_seq) as rownum from stats) s2
        on s1.rownum = s2.rownum + 1
    where s1.batterid != s2.batterid or s2.batterid is null
    )
    select
        game_pk,
        rec_seq,
        batterid,
        (select max(rownum) from GroupSets gs where gs.Rec_Seq <= s1.rec_seq) as PlayNumber,
        eventtype
    from
        stats s1;
    

    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 the MIN(REC_SEQ) per BatterId group in order to use REC_SEQ to control the order. Something like this should do it:

    select
        s1.game_pk,
        s1.rec_seq,
        s1.batterID,
        dense_rank() over (order by s2.rec_seq) as PlayNumber,
        s1.EventType
    from
        stats s1
    join
        (select batterid, min(rec_seq) rec_seq
         from stats group by batterid) s2 on s1.batterid = s2.batterid
    order by
        rec_seq
    

    Demo: http://www.sqlfiddle.com/#!3/0682e/4

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I have some data like this: 1 2 3 4 5 9 2 6
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I would like to count the length of a string with PHP. The string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.