在pyspark或hive中查找两个连续状态之间的持续时间

vjhs03f7  于 2021-06-27  发布在  Hive
关注(0)|答案(1)|浏览(255)

我有一个如下所示的数据框,其中包含列: ID, STATE and TIMESTAMP . Dataframe按 ID and TIMESTAMP . 我们需要找出两者之间的时间间隔 state S1 to S2 .
注意:对于一个特定的id,我们可以在s1到s2之间有多个转换。状态总是以s1开始,以s2结束。
有关详细信息,请参阅所附图片:
输入为蓝色,预期输出为绿色

pdtvr36n

pdtvr36n1#

select id, 
        unix_timestamp(timestamp) - 
        unix_timestamp(lag(timestamp) over(partition by id order by timestamp)) as time_diff
        from table;

相关问题