Skip to content
Advertisement

How to number rows according to values in columns

Imagine I have an event log (ordered by UserID and Start, Start_of_previous_event is added using LAG(), inactive time = Start - Start_of_previous_event):

How can I add a column with a session_ids ? New session starts after 30 minutes of inactive time and for new UserID.

Session_id column for the above example:

Is there a way to avoid it if I want to group the resulting table like this:

Advertisement

Answer

You can assign the session with date arithmetic and a cumulative sum. Date arithmetic varies by database, but this should give you the idea:

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