SQL: How to increment a parent group number when the child window has incrementing values?

Spark SQL 3.2.0

DB Fiddle

query output

Please see the above link for a simplified example of my dataset and desired outcome. In abstract, I have a dataset with a series of related events that can be grouped by their time order and event number. When ordering by time and event number, every time the event number resets to 1, you’re looking at a new set of events. I understand how to use row_number() or dense_rank() to increment event_group_number where sub_event_number = 1, but I’m uncertain how to make the rows where sub_event_number > 1 take on the correct event_group_number.

I’m currently doing the following:

case 
    when sub_event_number = 1 and is_event_type
    then row_number() over (partition by context_id, event_id, sub_event_number order by is_event_type asc, start_time asc) - 1
    else null
    end as event_group_number

I’d be grateful for any help, and I’m happy to answer any questions. Thank you!

A Complete Guide to Diamond Shapes and Cuts: The Ultimate Buyer’s Insight
Fashion

A Complete Guide to Diamond Shapes and Cuts: The Ultimate Buyer’s Insight

When shopping for a diamond, whether it’s for an engagement ring, a pendant, or a pair of earrings, the first thing people often notice is the sparkle. But what truly defines that sparkle? The answer lies in diamond shapes and cuts. These two aspects influence not only the appearance of the diamond but also its […]

Read More
hair store near me
Fashion

Find the Best Hair Store Near Me: Luvme Hair’s Loose-Wave Ponytail Extension

When you’re on the hunt for a “hair store near me,” you deserve a gem that sparkles with quality. Enter Luvme Hair—a treasure trove of hair wonders. Their Loose-Wave Ponytail Extension reigns supreme for anyone craving effortless elegance. With just a flick, you can transform your look into a polished masterpiece. This extension adds volume […]

Read More
Suit Alterations: The Craft of Tailoring for an Ideal Fit
Fashion

Suit Alterations: The Craft of Tailoring for an Ideal Fit

With regards to establishing a long term connection, there’s not a viable replacement for a well-fitting suit. A suit that fits appropriately can hoist your style, increment your certainty, and make you stand apart at any occasion. Nonetheless, few out of every odd suit is made to fit each body shape impeccably, which is where […]

Read More