tsql - Pyspark 中的多个 WHEN 条件实现

标签 tsql pyspark apache-spark-sql case-when .when

我的 T-SQL 代码低于我在 Pyspark 中转换的代码,但给了我错误

CASE
            WHEN time_on_site.eventaction = 'IN' AND time_on_site.next_action = 'OUT' AND time_on_site.timespent_sec < 72000 THEN 1  --  20 hours 
            WHEN time_on_site.eventaction = 'IN' AND time_on_site.next_action = 'OUT' AND time_on_site.timespent_sec >= 72000 THEN 0
            WHEN time_on_site.eventaction = 'IN' AND time_on_site.next_action = 'IN' AND time_on_site.timespent_sec <= 28800 THEN 2  -- 8 hours
            WHEN time_on_site.eventaction = 'IN' AND time_on_site.next_action = 'IN' AND time_on_site.timespent_sec > 28800 THEN 3
            WHEN time_on_site.type_flag = 'TYPE4' THEN 4
            ELSE NULL
         END AS "type"
下面是我的 Pyspark 脚本,它抛出了一个错误
from pyspark.sql.functions import when

TOS=TOS.withColumn('type', F.when( (col('eventaction') == 'IN') & (col('next_action') == 'OUT') & ("timespent_sec < 72000") , 1).
                            when( (col('eventaction') == 'IN') & (col('next_action') == 'OUT') & ("timespent_sec >= 72000") , 0).
                            when( (col('eventaction') == 'IN') & (col('next_action') == 'IN') & ("timespent_sec <= 28800") , 2).
                            when( (col('eventaction') == 'IN') & (col('next_action') == 'IN') & ("timespent_sec > 28800") , 3).
                            when(col('type_flag')=='TYPE4', 4).otherwise('NULL')
                            )
我哪里错了!?

最佳答案

我不知道 T-SQL 语法,但如果你想做 if:.. elif: ...elif.... else ,那么下面的代码将起作用。

from pyspark.sql.functions import when, col

TOS=TOS.withColumn('type', when( (col('eventaction') == 'IN') & (col('next_action') == 'OUT') & ("timespent_sec < 72000") , 1).
                            otherwise( when(   (col('eventaction') == 'IN') & (col('next_action') == 'OUT') & ("timespent_sec >= 72000") , 0).
                            otherwise( when(   (col('eventaction') == 'IN') & (col('next_action') == 'IN') & ("timespent_sec <= 28800") , 2).
                            otherwise( when(   (col('eventaction') == 'IN') & (col('next_action') == 'IN') & ("timespent_sec > 28800") , 3).
                            otherwise( when(   col('type_flag')=='TYPE4', 4).otherwise('NULL'))))))

关于tsql - Pyspark 中的多个 WHEN 条件实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54816878/

相关文章:

sql - 求和列具有空值时如何获取空值

python - 如何一次转换多个 Spark 数据框列类型?

apache-spark - Spark : PartitionBy, 更改输出文件名

apache-spark - PySpark 如何将 CSV 读入 Dataframe 并对其进行操作

sql-server - 需要 SQL Server 2008 建议中的 LIKE 函数

sql - 如何使用 Sql :Variable in xquery 从 XML 属性获取值

sql - 如何在不使用 JOIN 的情况下将 2 个表中的列合并为 1 个表

python - 为什么 1 行 DataFrame 上的 collect() 使用 2000 个执行器?

python-3.x - 如何划分两个聚合总和数据帧

apache-spark - PySpark:向 DataFrame 添加更多列的最佳实践