我试图让第一步时间戳到秒步长,以计算在两个步骤post:/login_requestpost:/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  |                                                                       
*-----------------------------------------------------------------------------------------* 

我的问题和100:

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

有没有人能给我一些指导,告诉我怎样才能达到以上的效果.

推荐答案

如果为‘POST:/LOGIN_REQUEST’,则需要判断方法,并将结果设置为空

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
device_serial device_gen status_code methods event_time first_step_ts
ABC345 i13 200 post:/login_request 3/3/24 23:10:05 null
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 null
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 null
EFG456 i13 200 post:/login 3/3/24 21:58:48 3/3/24 21:58:44
SELECT 6

fiddle

Sql相关问答推荐

Oracle SQL对列进行汇总并在列表底部显示总计

使用group by后我的平均输出不是我想要的

Trino/Presto sq:仅当空值位于组中第一个非空值之后时,才用值替换空值

基于前面行的值:当x&>2时重复1,当连续3行x=0时则重复0

使用占位符向SQL INSERT查询添加 case

为什么两个不同的窗口函数给出不同的排序结果?

数组列的postgres更新查询

关于Postgres横向联接的谓词

根据时间、状态和相关行在PostgreSQL中的存在来删除行

表函数的作用域和功能

Haystack针相交-在元素最多的Haystack中查找集合

如何在android房间中进行多个加入

用替代方案替换 SQL Cursor 以提高性能

使用临时表判断记录是否存在 - 如果存在则执行相同的操作

SQL 如何根据当前事件和下一个事件确定操作的持续时间?

如何通过CROSS APPLY获取多级嵌套JSON属性的值?

joins 组合多个重复数据删除策略

使用 PL/PGSQL 函数 Select 返回多条记录

PostgreSQL 如何在一组项目及其数量上找到完全相同的订单?

创建一个将层次 struct 级别放入列中的查询