我在数据湖中有一些数据:
Person | Date | Time | Number of Friends |
Bob | 02/01 | unix_ts1 | 5 |
Kate | 02/01 | unix_ts2 | 2 |
Jill | 02/01 | unix_ts3 | 3 |
Bob | 02/01 | unix_ts3 | 7 |
Kate | 02/02 | unix_ts4 | 10 |
Jill | 01/29 | unix_ts0 | 1 |
我想产生一个像这样的表:
Person | Date | Time | Number of Friends DELTA | Found Diff Between
Bob | 02/01 | unix_ts1 | NaN | (5, NaN)
Kate | 02/01 | unix_ts2 | NaN | (2, NaN)
Jill | 02/01 | unix_ts3 | 2 | (3, 1)
Bob | 02/01 | unix_ts3 | 2 | (7, 5)
Kate | 02/02 | unix_ts4 | 8 | (10, 2)
因此,我有一个表,其中的每一行都由一个人的名字和记录数据的时间来标识。我想要一个查询,它将查找“ Bob”的实例,并为连续的时间戳查找增量,然后给出增量,以及找到差异之间的两个值。我希望每个人都能做到这一点。
我找到了一种方法,使用lag()命令在只有一个值的情况下执行此操作,但不会按Person进行匹配。如果下载了数据,我也知道如何在Pandas中执行此操作,但是我想知道Hive中是否可以执行此操作。
有没有办法做到这一点?谢谢!
答案 0 :(得分:1)
使用lag
窗口功能。
select person
,date
,time
,num_friends-lag(num_friends) over(partition by person order by time) as delta
,concat_ws(',',num_friends,lag(num_friends) over(partition by person order by time)) as found_diff_between
from tbl