Skip to content
Advertisement

SQL cummulative sum by group

I am struggling in finding a solution to create a cumulative sum by group in SQL.

I have a table which have different events. What I want to accomplish is to sum the duration time of an event.

What I need is a cumulative sum that gives me the total duration of Event1 as column besides ‘Event1_Start’ row. I would like to sum all the intermediates events before my event_start and event_end.

EventID EventName EventTime CumSumEvent1
1 Event1_Start 0.2 1.6
2 Event2_Start 0.3 0
3 Event3_End 0.2 0
4 Event2_End 0.6 0
5 Event4_Start 0 0
2 Event2_Start 0.3 0
6 Event1_End 0 0

I try to use sum over partition but no luck and CTE.

Thanks in advance for your suggestions. Best regards.

Advertisement

Answer

you can group by part of the string:

db<>fiddle here

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement