zl程序教程

您现在的位置是:首页 >  数据库

当前栏目

sql lead

SQL lead
2023-09-14 09:09:30 时间

WITH data AS
 (SELECT 1000088101 as player_id, 15449 as value_1, 1 as action,'2020-06-01 08:29:25.540 UTC' as timestamp
  UNION ALL SELECT 1000088101, 15449, 1,'2020-06-01 08:29:30.382 UTC'
   )
select player_id, value_1, action, LEAD(timestamp,1) OVER (PARTITION BY player_id  ORDER BY timestamp ASC) next_time, timestamp,
from data

就是将数据顺序相连,便于计算时间差,如图可见 ,该用户有两条数据,将下一条数据,放到当前

player_idvalue_1actionnext_timetimestamp ,
110000881011544912020-06-01 08:29:30.382 UTC2020-06-01 08:29:25.540 UTC
11000088101154491null2020-06-01 08:29:30.382 UTC