pyspark查询case语句抛出错误

2guxujil  于 2023-11-16  发布在  Spark
关注(0)|答案(1)|浏览(105)

我正在运行下面的pyspark查询
它抛出下面的错误。我希望动态值通过一个变量传入case语句

data1 = 'HI'
 data2 = 'HELLO'

 spark.sql(f""" select  Initials,
                case when Initials  in ('AM','NJ') then {data1} else {data2} end as s  
                from omegawriter""").display()

字符串
我得到下面的错误

A column or function parameter with name `HI` cannot be resolved.
  Project [Initials#69, CASE WHEN Initials#69 IN (AM,NJ) THEN 'HI ELSE 'HELLO END AS s#57]


我不明白这个查询有什么问题。有人能帮助我吗

luaexgnf

luaexgnf1#

纯文本值需要加引号。

spark.sql(f""" select  Initials,
                case when Initials  in ('AM','NJ') then '{data1}' else '{data2}' end as s  
                from omegawriter""").display()

字符串

相关问题