Hive 跟踪列中的更改

Hive Track changes in a column

您好,我一直在尝试监视与配置单元列中原始值的差异。例如:

column 1  tracking_column
6          0
6          0
6          0
5          -1
6          0
6          0
7          1
8          2

我一直在使用滞后函数,但这似乎只允许我跟踪从一行到下一行的变化,而不保留 运行 计数。因此,当数字从 6 上升到 7,然后上升到 8 时。滞后或领先我认为在这种情况下不起作用。

感谢任何提示。干杯

我想你想要first_value():

select col1, (first_value(col1) over (order by ?) - col1) as diff
from t;

? 是指定 table 顺序的列的占位符。

您也可以使用 cross join:

select t.col1, (t.col1 - t1.col1) as diff
from t cross join
     (select t.*
      from t
      order by ?
      limit 1
     ) t1;