简体   繁体   English

SPARK SQL CASE何时> 0

[英]SPARK SQL CASE WHEN > 0

I have been struggling with this for about 3 hours. 我已经为此苦苦挣扎了大约3个小时。 Running Spark 1.6 运行Spark 1.6

Trying to get this to work in the spark SQl context. 试图使其在Spark SQl上下文中起作用。 evt_acct_app_id is an integer, why is this not working, in sql this is easy. evt_acct_app_id是一个整数,这为什么不起作用,在sql中这很容易。 I tried multiple variations of this, remove apostrophe and etc. 我尝试了多种变体,删除了撇号等。

CASE evt_acct_app_id
 WHEN evt_acct_app_id > '0' THEN '001'
 ELSE '002'
 END
 AS EVNT_SUBTYPE_CD,

Keep getting this error:Got this unknown exception: 继续收到此错误:得到了这个未知的异常:

org.apache.spark.sql.AnalysisException: cannot resolve 'CASE evt_acct_app_id WHEN (cast(evt_acct_app_id as double) > cast(0 as double)) THEN 001 ELSE 002' 
due to data type mismatch: key and WHEN expressions should all be same type or coercible to a common type;

Don't use single quotes if you are comparing to an integer: 如果要与整数比较,请不要使用单引号:

(CASE WHEN evt_acct_app_id > 0 THEN '001'
      ELSE '002'
 END) as EVNT_SUBTYPE_CD,

In addition, when you have expressions for comparison, the correct syntax for CASE does not have a column name after the CASE . 此外,当你有一个比较表达式,正确的语法CASE不具备后一列名CASE

Try below: 请尝试以下方法:

CASE WHEN evt_acct_app_id > '0' THEN '001' ELSE '002' END AS EVNT_SUBTYPE_CD, evt_acct_app_id>'0'然后'001'ELSE'002'结束时为EVNT_SUBTYPE_CD,

Try this: 尝试这个:

def evtChange(d:Column) = {when(d > 0,"001").otherwise("002")}

data.select( evtChange($"evt_acct_app_id").as("EVNT_SUBTYPE_CD") )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM