Generating SessionID - Incrementing From Last Value

Viewed 23

I have a table with millions of rows that gets updated nightly from the previous days data.

I have a query that recognizes time gaps between a users last interaction with the system. If the time between their last interaction is more than 30 minutes it is identified as "New Session=1"

I need to add an incrementing SessionID that picks up the latest sessionID (for each user), adds one, then continues on from that point.

My current code works great as a query, but due to the size of the table I need to store the SessionID in a column within the table. I can't figure out how to "pick up from where it left off" when new data is entered into the table nightly.

Here's a sample dataset which would be added nightly

with rawData as(
Select '9d12ea3c' 'UserID',108906620 'RequestID',   '2021-01-25 04:07:03.1433333' CreatedDate,  1 'NewSession'
union all Select '9d12ea3c', 108906621,'2021-01-25 04:07:03.2900000',0
union all Select '9d12ea3c',108906623,'2021-01-25 04:07:03.5533333',0
union all Select '9d12ea3c',108906626,'2021-01-25 05:07:04.5533333',1
union all Select '9d12ea3c',108906629,'2021-01-25 05:07:05.5533333',0
union all Select '9d12ea3c',108906630,'2021-01-25 08:07:06.5533333',1
union all Select '9d12ea3c',108906632,'2021-01-25 08:07:10.5533333',0
union all Select '9d12ea3c',108906634,'2021-01-26 08:07:16.5533333',1
union all Select '9d12ea3c',108906635,'2021-01-28 09:07:26.5533333',1
)

select
c.RequestID
,c.CreatedDate
,c.NewSession
,sum(c.NewSession) over (partition by c.UserID order by c.CreatedDate rows between unbounded preceding and current row) SessionID
from rawData c

Here's what the output looks like

RequestID CreatedDate NewSession SessionID
108906620 2021-01-25 04:07:03.1433333 1 1
108906621 2021-01-25 04:07:03.2900000 0 1
108906623 2021-01-25 04:07:03.5533333 0 1
108906626 2021-01-25 05:07:04.5533333 1 2
108906629 2021-01-25 05:07:05.5533333 0 2
108906630 2021-01-25 08:07:06.5533333 1 3
108906632 2021-01-25 08:07:10.5533333 0 3
108906634 2021-01-26 08:07:16.5533333 1 4
108906635 2021-01-28 09:07:26.5533333 1 5
0 Answers
Related