SPARK SQL CASE WHEN > 0 - sql

I have been struggling with this for about 3 hours.
Running Spark 1.6
Trying to get this to work in the spark SQl context. evt_acct_app_id is an integer, why is this not working, in sql this is easy. 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.

Try below:
CASE
WHEN evt_acct_app_id > '0' THEN '001'
ELSE '002'
END
AS 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") )

Related

Operator 'is true' for type 'long' not found for oracle sql query

I am trying to run this logic, where i get output as follows,
My code to get the below output field is shown below
select max(dtm) over (partition by name ,id )-current_date from mm
output
-4168
-4168
-4168
-4127
what i want is to run this logic along with 'case when' statement so i tried:
case when max(dtm) over (partition by name ,id )-current_date then 'yes'
else 'No' end as output
from mm
but i get an error as follows, not sure what went wrong in this logic.
Operator 'is true' for type 'long' not found
There are two forms of CASE expression. One is referred to as simple case expression and the other is referred to as searched case expression. The SQL in your question uses the latter, i.e. searched case expression. I believe you probably need simple case expression, i.e.
select case max(dtm) over (partition by name ,id ) - current_date
when -4168 then 'Yes'
else 'No'
end as answer
from mm
select
case when current_date-max(dtm) over (partition by name ,id ) < 30 then 'yes'
else 'No' end as 'output'
from mm

Select with IF statement on postgresql

I have a code like that:
select
tbl.person
,COUNT(distinct tbl.project)
,if (tbl.stage like '%SIGNED%') then sum(tbl.value) else '0' end if as test
from
my_table tbl
group by
1
And it returns me that error message:
SQL Error [42601]: ERROR: syntax error at or near "then"
I didn't got it. As I saw on documentation, the if statement syntax appears to be used correctly
IF is to be used in procedures, not in queries. Use a case expression instead:
select
tbl.person
,COUNT(distinct tbl.project)
,sum(case when tbl.stage like '%SIGNED%' then tbl.value else 0 end) as test
from
my_table tbl
group by
1
Notes:
tbl.stage is not part of the group by, so it should most probably be enclosed within the aggregate expression, not outside of it
all values returned by a case expression need to have the same datatype. Since sum(tbl.value) is numeric, the else part of the case should return 0 (number), not '0' (string).
In Postgres, I would recommend using filter:
select tbl.person, COUNT(distinct tbl.project)
sum(tbl.value) filter (where tbl.stage like '%SIGNED%') as test
from my_table tbl
group by 1;
if is control flow logic. When working with queries, you want to learn how to think more as sets. So the idea is to filter the rows and add up the values after filtering.
replace
if (tbl.stage like '%SIGNED%') then sum(tbl.value) else '0' end if as test
with
sum(case when tbl.stage like '%SIGNED%' then tbl.value end) as test

Unable to write case statement in Spark SQL

I have written below query in Spark SQL using spark-shell and I am getting below error message
spark.sql(""" select case when Treatment == 'Yes' then 1 else 0 end AS 'All-Yes' from person """)
Error message-
org.apache.spark.sql.catalyst.parser.ParseException:
mismatched input ''All-Yes'' expecting <EOF>(line 1, pos 58).
Can someone please help me in this
The alias should be enclosed with backquotes
select case when Treatment == 'Yes' then 1 else 0 end AS `All-Yes` from person
though in general you shouldn't use non-standard, an incompatible names.

Casting a String Column to 3 Digit scale in Hive Query Language

I have a select statement as follows
SELECT t5.Name AS ProductName
, CAST (t5.salerevenue AS DECIMAL(20,3)) AS Revenue
, CASE WHEN t5.PreviousRevenue <> 0 THEN CAST(t5.PresentRevenue/t5.PreviousRevenue*100 AS STRING)
ELSE 'NA'
END AS RevenueTrend
I want to cast the RevenueTrend as a decimal with maximum 3 point scale (20.123)
I have to cast it as String because WHEN t5.PreviousRevenue <> 0 is not met I have to show it as 'NA'
I tried doing it as follows
CASE WHEN t5.PreviousRevenue <> 0 THEN CAST((CAST(t5.PresentRevenue/t5.PreviousRevenue*100) AS DECIMAL(20,3)) AS STRING)
ELSE 'NA'
END AS RevenueTrend
but I am getting a syntax error.
The revenue part is projected as expected. I want the revenuetrend part also to be projected like revenue. Is there any way to achieve this?
Try converting first to a decimal, then to a string:
SELECT . . .
(CASE WHEN t5.PreviousRevenue <> 0
THEN CAST(CAST(t5.PresentRevenue/t5.PreviousRevenue*100 AS DECIMAL(20,3)) as string)
ELSE 'N/A'
END) AS RevenueTrend

Error in SQL case statement when trying to create binary flag?

Here's my query where I'm testing my case structure:
SELECT TOP 1 CASE 130
WHEN '000000000000000' THEN '0'
WHEN '' THEN '0'
WHEN 'XXX' THEN '0'
WHEN 'RETIRED' THEN '0'
WHEN 'STUDENT' THEN '0'
ELSE '1'
END AS employed_flag
INTO #employedbeta
FROM CreditBureau.Experian
I'm just trying to make a new temporary table, but I'd like my case to work first. I keep getting the error:
Conversion failed when converting the varchar value 'XXX' to data type int.
In the database, the column 130 is a char, and I don't know why it thinks I want to make it a number. SQL server management studio, if it matters.
The column name is 130, I left the '1' off because I rewrote it here but I get the error regardless in my actual query.
130 is an integer literal. If that's really the column name, you'll have to escape it using double quotes. As a side note, you should probably return the same type (char) in the else branch too:
CASE "130"
WHEN '000000000000000' THEN '0'
WHEN '' THEN '0'
WHEN 'XXX' THEN '0'
WHEN 'RETIRED' THEN '0'
WHEN 'STUDENT' THEN '0'
ELSE '1'
END AS employed_flag
130 is a really bad column name. But, I would simplify the logic to:
SELECT TOP 1 (CASE WHEN [130] IN ('000000000000000', '', 'XXX', 'RETIRED', 'STUDENT')
THEN 0 ELSE 1
END) AS employed_flag
INTO #employedbeta
FROM CreditBureau.Experian;
Note that I also changed the employed_flag to a numeric value rather than a string. That makes more sense to me.