我想使用滞后函数并使其依赖于一个变量,但它给了我一个错误,因为它必须是整数类型。
我尝试了 lag(pivote,pivote) 但出现以下错误: AnalysisException:LEAD/LAG 的偏移参数必须是常量正整数:lag(pivote,pivote)
你还有其他选择吗?
代码如下:
select
*
,if ( Monto > 0
, 0
, lag( pivote, pivote ) OVER( partition by ID order by Fecha )
) as B
FROM(
select
*,
row_number() OVER( partition by ID order by Fecha ) as pivote
FROM table1
) as base
;
我想你可能在 lag() 中提到了
pivote
两次。你可以尝试下面的 SQL -
select
base.*
,if ( Monto > 0
, 0
, lag( base.pivote ) OVER( partition by ID order by Fecha )
) as B
FROM(
select
t.*,
row_number() OVER( partition by ID order by Fecha ) as pivote
FROM table1 t
) as base