Hive 当配置单元中的列B不为null时,从列A获取上一个值

Hive 当配置单元中的列B不为null时,从列A获取上一个值,hive,null,lag,Hive,Null,Lag,我在下面有一个表tableA ID number Estimate Client ---- ------ 1 3 8 A 1 NULL 10 Null 1 5 11 A 1 NULL 19 Null 2 NULL 20 Null 2 2

我在下面有一个表
tableA

ID   number   Estimate   Client    
----   ------
1      3          8         A 
1      NULL       10        Null
1      5          11        A      
1      NULL       19        Null 
2      NULL       20        Null
2      2          70        A   
.......
number
列不为空时,我想选择
Estimate
列的前一行。例如,当
number=3
时,则
pre\u estimate=NULL
;当
number=5
时,则
pre\u estimate=10
;当
number=2
时,则
pre\u estimate=20

下面的查询在配置单元中似乎没有返回正确答案。正确的方法应该是什么

select lag(Estimate, 1) OVER (partition by ID) as prev_estimate
from tableA
where number is not null

考虑具有以下结构的表:

number - int
estimate - int
order_column - int
order\u列
作为要对表行进行排序的列

表中的数据:

number   estimate   order_column
3          8         1 
NULL       10        2
5          11        3      
NULL       19        4 
NULL       20        5
2          70        6  
我使用了下面的查询,得到了您提到的结果

SELECT * FROM (SELECT number, estimate, lag(estimate,1) over(order by order_column) as prev_estimate from tableA) tbl where tbl.number is not null;
根据我的理解,我没有找到按id分区的原因,这就是为什么我没有在表中考虑id


您得到错误结果的原因是,主查询中的where子句将仅选择编号为NOTNULL的记录,然后计算滞后函数,但是,在计算滞后函数时,需要考虑所有行,然后您应该选择具有编号为NULL的行。

您的表没有定义行排序的列,因此“前一行”的概念不清楚。@ GMB什么是该排序列的示例?我可以添加一个,然后执行滞后操作以查找上一行吗?
滞后(估计值,1)超过(按ID顺序按某些列划分)作为上一个估计值。以下是GMB提到的一些问题。你需要找到答案。表格数据不清楚。@KoushikRoy对此有什么要求吗?我有一个timestamp列,可以按顺序排序。但我也试过了,所以给空值。