Reputation: 127
I am working on piece of tracking customer journey on a application. Trying to set a rank/counter to segregate a session among multiple sessions sharing same id.(Ideally should have been unique).
current structure of table:
desired output:
Have tried using row_number over sequence number, but when a sequence_number appears for first time( 4 in case of last row), it takes to be the first count.
How should I go about splitting the data and having a session column whenever the sequence number starts at 1( indicating new session).
Upvotes: 0
Views: 97
Reputation: 1269753
You seem to want a cumulative count of 'START'
s:
select t.*,
sum(case when activity = 'START' then 1 else 0 end) over
(partition by session_id
order by time_stamp
rows between unbounded preceding and current row
) as session
from t;
Upvotes: 1