我想在sparksql查询中运行case-when语句。
这是数据
object green
______ _____
1 true
2 false
3 true
这是查询
%sql
select
object,
case when green is true then 'A'
when green is false then 'B'
else 'C' end as case_when_statement
from my_table
此查询导致以下错误: mismatched input 'object' expecting <EOF>(line 3, pos 14)
我试过替换 is true
与 = TRUE
, = 'TRUE'
以及 is TRUE
1条答案
按热度按时间wd2eg0qa1#
尝试
==
是的。df.withColumn("case_when_statement", when(col("green") === true,lit("A")).when(col("green")===false,lit("B")).otherwise(lit("c"))).show()
//+------+-----+-------------------+
//|object|green|case_when_statement|
//+------+-----+-------------------+
//| 1| true| A|
//| 2|false| B|
//| 3| true| A|
//+------+-----+-------------------+