I am trying to get first step timestamp to seconds step to calculate how much time it took between two steps post:/login_request and post:/login using lag() but it's taking timestamp each step and add it next record.
*-----------------------------------------------------------------------------------------*
| device_serial | device_gen | status_code | method | event_time |
*-----------------------------------------------------------------------------------------*
| ABC345 | i13 | 200 | post:/login_request | 3/3/24 23:10:05 |
| ABC345 | i13 | 200 | post:/login | 3/3/24 23:10:10 |
| EFG456 | i13 | 200 | post:/login_request | 3/3/24 18:37:25 |
| EFG456 | i13 | 200 | post:/login | 3/3/24 18:37:28 |
| EFG456 | i13 | 200 | post:/login_request | 3/3/24 21:58:44 |
| EFG456 | i13 | 200 | post:/login | 3/3/24 21:58:48 |
*-----------------------------------------------------------------------------------------*
My Query and fiddle:
select
device_serial,
device_gen,
status_code,
method,
event_time,
lag(event_time) over(partition by device_serial, status_code order by event_time) as first_step_ts
from test_tbl
what I am getting:
*-----------------------------------------------------------------------------------------------------------*
| device_serial | device_gen | status_code | method | event_time | prev_ts |
*-----------------------------------------------------------------------------------------------------------*
| ABC345 | i13 | 200 | post:/login_request | 3/3/24 23:10:05 | |
| ABC345 | i13 | 200 | post:/login | 3/3/24 23:10:10 | 3/3/24 23:10:05 |
| EFG456 | i13 | 200 | post:/login_request | 3/3/24 18:37:25 | |
| EFG456 | i13 | 200 | post:/login | 3/3/24 18:37:28 | 3/3/24 18:37:25 |
| EFG456 | i13 | 200 | post:/login_request | 3/3/24 21:58:44 | 3/3/24 18:37:28 |
| EFG456 | i13 | 200 | post:/login | 3/3/24 21:58:48 | 3/3/24 21:58:44 |
*-----------------------------------------------------------------------------------------------------------*
what I actually want:
*-----------------------------------------------------------------------------------------------------------*
| device_serial | device_gen | status_code | method | event_time | prev_ts |
*-----------------------------------------------------------------------------------------------------------*
| ABC345 | i13 | 200 | post:/login_request | 3/3/24 23:10:05 | |
| ABC345 | i13 | 200 | post:/login | 3/3/24 23:10:10 | 3/3/24 23:10:05 |
| EFG456 | i13 | 200 | post:/login_request | 3/3/24 18:37:25 | |
| EFG456 | i13 | 200 | post:/login | 3/3/24 18:37:28 | 3/3/24 18:37:25 |
| EFG456 | i13 | 200 | post:/login_request | 3/3/24 21:58:44 | |
| EFG456 | i13 | 200 | post:/login | 3/3/24 21:58:48 | 3/3/24 21:58:44 |
*-----------------------------------------------------------------------------------------------------------*
can anyone help to give me some guidane on how can I achieve above result.
You need to check methods if it is 'post:/login_request' and set the result to NULL
fiddle