Hello,
I’m having sort of a struggle querying a table which contains events in the form of “timestamp” and “start/stop” records.
To better illustrate my problem, I’ve created a small table with some data (please see attachment). The demo data consists of a “customer” (representing any business key) and a timestamp marked as either “start” or “stop” event type. My goal is to get the actual duration between a “start” and a “stop” event/record.
The date calculation itself is not a problem and it’s easy to enumerate the events. However, I don’t know how to “reset” the group id, if there are more than one “roundtrips” (pairs of start and stop)
Customers 1000 and 2000 show the most simple case, there’s one, resp. two pairs of a start and a stop event. These work well for me.
My base idea was to just assign row numbers, so “start 1” goes to “stop 1” and so on. However to simulate some “noise” I’ve also included some examples which contain multiple start or stop events. In this situation, I want to pick the first “start” and the last “stop” of a group.
If there’s only one “start”, I’d like to assume the current date (sysdate) as the “stop” event. Invalid constellations (see comments in script) would be ignored/filtered out; eg. If there’s no “start” in a group or “stop” before “start”
Anyone knows how to do this? I was thinking of CTEs each dealing with one situation, but I’m not sure if that’s a performance killer.
Thanks in advance for any help :-)
eventstream.txt (3.34 KB)