I have db on MySQL – dataset with users actions logs. We have next data collection logic – every user have it own user_id and we record log of event actions during user session.
user_id | session_id | dateTime | event |
---|---|---|---|
1 | aa | 2023-01-01 13:12:11 | login |
1 | aa | 2023-01-01 14:12:10 | buy |
1 | bb | 2023-01-02 11:12:10 | page |
2 | cc | 2023-01-01 10:11:01 | login |
2 | gg | 2023-01-03 11:12:11 | logout |
2 | gg | 2023-01-03 13:11:03 | click |
2 | gg | 2023-01-03 14:10:07 | logout |
The main goal is prepare mysql querry with raw data and add 2 new calculated columns: 1. with events action sequence during every user session and 2. with session sequence during all user lifetime by SQL.
Expected output:
user_id | session_id | dateTime | event | event_seq | session_seq |
---|---|---|---|---|---|
1 | aa | 2023-01-01 13:12:11 | login | 1 | 1 |
1 | aa | 2023-01-01 14:12:10 | buy | 2 | 1 |
1 | bb | 2023-01-02 11:12:10 | page | 1 | 2 |
2 | cc | 2023-01-01 10:11:01 | login | 1 | 1 |
2 | gg | 2023-01-03 11:12:11 | logout | 1 | 2 |
2 | gg | 2023-01-03 13:11:03 | click | 2 | 2 |
2 | gg | 2023-01-03 14:10:07 | logout | 3 | 2 |
3
Answers
you can use a query like this:
sample
note: see if your MySQL Version supports WINDOW functions
you can accomplish this using window functions.
DB fiddle
You can use window functions
ROW_NUMBER()
andDENSE_RANK()
:ROW_NUMBER()
enumerates rows inside of each partition which is grouped byuser_id, session_id
columnsDENSE_RANK()
consequently ranks rows inside each partition which is grouped byuser_id
column. The order is determined inORDER BY user_id, session_id
expression.The documentation.