【发布时间】:2020-09-09 21:23:53
【问题描述】:
我需要计算 hive 中序列号的各种交互之间的差异。 我的桌子有多个序列号,有多个交互。我已经根据时间戳对序列号的交互进行了排序。我现在也有兴趣为连续行添加一个时间戳不同的列。
例如,图像中的最后一列显示了预期的输出。
感谢您的帮助。
【问题讨论】:
标签: sql datetime hive hiveql window-functions
我需要计算 hive 中序列号的各种交互之间的差异。 我的桌子有多个序列号,有多个交互。我已经根据时间戳对序列号的交互进行了排序。我现在也有兴趣为连续行添加一个时间戳不同的列。
例如,图像中的最后一列显示了预期的输出。
感谢您的帮助。
【问题讨论】:
标签: sql datetime hive hiveql window-functions
您可以使用lag() 访问给定分区中的“上一个”行:
select
t.*,
lag(timestamp) over(partition by sr_no order by timestamp) as lag_timestamp
from mytable t
假设你想要两个时间戳之间的秒差,那么:
select
t.*,
unix_timestamp(timestamp)
- unix_timestamp(lag(timestamp) over(partition by sr_no order by timestamp))
as diff_seconds
from mytable t
【讨论】: