I've got the following ordered by user_id, timestamp table:
| user_id | timestamp |
|---|---|
| 1000 | 1661919816 |
| 1000 | 1661919861 |
| 1001 | 1661919816 |
I need to enumerate this table with session id by the following rules:
- When diff between two timestamps is less than 1 minute, then it is the same session
- When diff is more than 1 minute, then it is previous session_id + 1
- When user_id changes, so does session_id (again previous session_id + 1)
So in my case I want something like this:
| user_id | timestamp | session_id |
|---|---|---|
| 1000 | 1661919816 | 1 |
| 1000 | 1661919861 | 1 |
| 1001 | 1661919816 | 2 |
I need something like dense_rank(), however I don't know how to apply it to diff between two timestamps