通过更新同一列来实现滞后函数

问题描述 投票:0回答:1

我要将条形码(offset=1)的滞后值更新为条形码

case 
  when ( lag(barcode,1) over (order by barcode ) 
        and  Datediff(SS, eventdate,lag(next_eventdate,1) over (order by barcode)) < 3*3600 ) 
  THEN 1 
  ELSE 0 
END as FLAG 

我在pyspark上实现了它,但是给了我一个错误

from pyspark.sql.functions import col, unix_timestamp
timeDiff = unix_timestamp('eventdate', format="ss")- unix_timestamp(F.lag('next_eventdate', 1), format="ss")
ww= Window.orderBy("barcode") 
Tgt_df_tos = Tgt_df_7.withColumn('FLAG',F.when((F.lag('barcode', 1)) & ( timeDiff <= 10800),"1").otherwise('0'))   

我得到的错误

AnalysisException: "cannot resolve '(lag(`barcode`, 1, NULL) AND ((unix_timestamp(`eventdate`, 'ss') - unix_timestamp(lag(`next_eventdate`, 1, NULL), 'ss')) <= CAST(10800 AS BIGINT)))' due to data type mismatch: differing types in '(lag(`barcode`, 1, NULL) AND ((unix_timestamp(`eventdate`, 'ss') - unix_timestamp(lag(`next_eventdate`, 1, NULL), 'ss')) <= CAST(10800 AS BIGINT)))' (int and boolean).
tsql pyspark pyspark-sql lag lead
1个回答
1
投票

我不熟悉pyspark,但在我看来问题出在CASE声明中。

CASE WHEN (
        LAG(barcode,1) OVER (ORDER BY barcode ) 
    AND
        DATEDIFF(SS, eventdate, LAG(next_eventdate, 1) OVER(ORDER BY barcode)) < 3*3600
)

有两个表达式:“LAG(条形码,1)OVER(ORDER BY条形码)”,它评估为INTEGER。

“DATEDIFF(SS,eventdate,LAG(next_eventdate,1)OVER(ORDER BY条形码))<3 * 3600”,其评估为布尔值(由于不等式)。

这些表达式与AND运算符结合使用,通常用于组合两个布尔表达式。我相信这是错误的原因。

LAG(条形码,1)OVER(ORDER BY条形码)评估为INTEGER而不是布尔值。

所以表达式看起来像:

CASE WHEN (324857 AND True) THEN 1 ELSE 0 END as FLAG

AnalysisException: "cannot resolve .... (int and boolean).
© www.soinside.com 2019 - 2024. All rights reserved.