【问题标题】:Implementation of lag function by updating the same column通过更新同一列实现滞后功能
【发布时间】:2019-03-01 07:39:16
【问题描述】:

我必须将条形码 (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】:

我不熟悉 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(barcode,1) OVER (ORDER BY barcode)" 计算结果为 INTEGER。

"DATEDIFF(SS, eventdate, LAG(next_eventdate, 1) OVER(ORDER BY barcode))

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

LAG(barcode,1) OVER (ORDER BY barcode) 计算结果为 INTEGER 而不是布尔值。

所以表达式看起来像:

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

AnalysisException: "cannot resolve .... (int and boolean).

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多