使用 `lag()` 获取之前的时间戳

问题描述 投票:0回答:1

我正在尝试使用

post:/login_request
获取第一步时间戳到秒步,以计算两个步骤
post:/login
lag()
之间花费的时间,但它会在每个步骤中获取时间戳并将其添加到下一条记录。

*-----------------------------------------------------------------------------------------*
| 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  |                                                                       
*-----------------------------------------------------------------------------------------* 

我的查询和小提琴

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

我得到了什么:

*-----------------------------------------------------------------------------------------------------------*
| 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 |
*-----------------------------------------------------------------------------------------------------------* 

我真正想要的:

*-----------------------------------------------------------------------------------------------------------*
| 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 |
*-----------------------------------------------------------------------------------------------------------* 

任何人都可以帮我提供一些关于如何实现上述结果的指导吗?

sql postgresql presto
1个回答
0
投票

您需要检查方法是否为 'post:/login_request' 并将结果设置为 NULL

select
    device_serial, 
    device_gen, 
    status_code, 
    methods, 
    event_time,
  CASE WHEN  methods = 'post:/login_request' THEN NULL ELSE 
    lag(event_time) over(partition by device_serial, status_code order by event_time) END as first_step_ts
from test_tbl
设备_串行 设备生成 状态代码 方法 活动时间 first_step_ts
ABC345 i13 200 帖子:/login_request 3/3/24 23:10:05
ABC345 i13 200 帖子:/登录 3/3/24 23:10:10 3/3/24 23:10:05
EFG456 i13 200 帖子:/login_request 3/3/24 18:37:25
EFG456 i13 200 帖子:/登录 3/3/24 18:37:28 3/3/24 18:37:25
EFG456 i13 200 帖子:/login_request 3/3/24 21:58:44
EFG456 i13 200 帖子:/登录 3/3/24 21:58:48 3/3/24 21:58:44
SELECT 6

小提琴

© www.soinside.com 2019 - 2024. All rights reserved.