sql - 用户交易时间差
问题描述
Table: txn
customer_id | time_stamp
-------------------------
1 | 00:01:03
1 | 00:02:04
2 | 00:03:05
2 | 00:04:06
希望查询 customer_id 的每笔第一笔交易和下一笔交易之间的时间差
结果:
客户编号 | 时差
1 | 61
从 txn 中选择 customer_ID, ...
解决方案
你想要lead()
。. . 但众所周知,日期/时间函数是特定于数据库的。在 SQL Server 中:
select t.*,
datediff(second,
time_stamp,
lead(time_stamp) over (partition by customer_id order by time_stamp)
) as diff_seconds
from t;
在 BigQuery 中:
select t.*,
timestamp_diff(time_stamp,
lead(time_stamp) over (partition by customer_id order by time_stamp),
second
) as diff_seconds
from t;
推荐阅读
- github - Plesk 黑曜石,Github 克隆不工作
- angular - 使用 ChangeDetectionStrategy.OnPush 的角度单元测试不起作用
- variables - 如何基于另一个不同长度的数据集在 R 中创建变量
- ios - 打开相机时应用程序崩溃
- java - 查找一个项目在数组中出现了多少次 = 解释代码
- javascript - ReferenceError:确认未定义
- android - 同步“com.google.firebase:firebase-analytics@17.2.2”时生成错误
- macos - Macbook上的Powershell脚本返回错误
- python - os.walk() 在 Windows 10 上为带有重音字母的文件生成无效路径
- c++ - C2679 二进制“<<”:未找到采用右侧操作数类型的运算符