SQL:具有可变参数的 LAG
我想使用滞后函数并使其依赖于一个变量,但它给了我一个错误,因为它必须是整数类型。
我尝试了 lag(pivote,pivote) 但出现以下错误: AnalysisException: LEAD/LAG 的偏移参数必须是常量正整数: lag(pivote,ivote)
您有其他选择吗?
代码如下:
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
;
I want to use the lag function and make it depend on a variable, but it gives me an error because it must be of type integer.
I tried lag( pivote, pivote ) but I get the following error:
AnalysisException: The offset parameter of LEAD/LAG must be a constant positive integer: lag(pivote, pivote)
Do you have any alternative?
The code is the following:
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
;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想你可能在 lag() 中提到了两次
pivote
。你可以尝试下面的 SQL -I think you probably mentioned
pivote
twice in lag(). can you please try below SQL -