You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@spark.apache.org by Antony Mayi <an...@yahoo.com.INVALID> on 2015/05/16 16:21:08 UTC
IF in SQL statement
Hi,
is it expected I can't reference a column inside of IF statement like this:
sctx.sql("SELECT name, IF(ts>0, price, 0) FROM table").collect()
I get an error:
org.apache.spark.sql.AnalysisException: unresolved operator 'Project [name#0,if ((CAST(ts#1, DoubleType) > CAST(0, DoubleType))) price#2 else 0 AS c1#378];
it works ok if I use value instead of column ref:
sctx.sql("SELECT name, IF(ts>0, 1, 0) FROM table").collect()
thx,Antony.
Re: IF in SQL statement
Posted by ayan guha <gu...@gmail.com>.
Try like this
SELECT name, case when ts>0 then price else 0 end from table
On Sun, May 17, 2015 at 12:21 AM, Antony Mayi <an...@yahoo.com.invalid>
wrote:
> Hi,
>
> is it expected I can't reference a column inside of IF statement like this:
>
> sctx.sql("SELECT name, IF(ts>0, price, 0) FROM table").collect()
>
> I get an error:
>
> org.apache.spark.sql.AnalysisException: unresolved operator 'Project
> [name#0,if ((CAST(ts#1, DoubleType) > CAST(0, DoubleType))) price#2 else 0
> AS c1#378];
>
> it works ok if I use value instead of column ref:
>
> sctx.sql("SELECT name, IF(ts>0, 1, 0) FROM table").collect()
>
> thx,
> Antony.
>
>
--
Best Regards,
Ayan Guha