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!

misconception
Fashion

The Future of Lab-Grown Diamonds: A New Era in Elegance and Ethics

In recent years, the future of lab grown diamonds have emerged as a transformative force in the jewelry industry. As consumers increasingly prioritize sustainability and ethical sourcing, these remarkable gems offer a compelling alternative to their mined counterparts. This article explores the future of lab-grown diamonds, highlighting their unique characteristics, market trends, and the technological […]

Read More
Fashion

Diamantes Sintéticos: A Comprehensive Guide to Lab-Made Diamonds

Diamantes sintéticos, also known as lab-made diamonds, have become a popular alternative to natural diamonds in recent years. These diamonds are created using advanced technological processes that replicate the conditions under which natural diamonds form. Unlike natural diamonds, which take millions of years to form deep within the Earth, lab-made diamonds can be produced in […]

Read More
Lab Grown Diamonds
Fashion

Understanding Lab Grown Diamonds: A Modern Choice in Fine Jewelry

What Are Lab Grown Diamonds? Lab grown diamonds, also known as manufactured or refined diamonds, are created in a laboratory climate utilizing advanced technological cycles that replicate the natural diamond formation. Dissimilar to mined diamonds, which are formed more than huge number of years under outrageous tension and temperature, lab grown diamonds are delivered in […]

Read More