I have this below case statement in postgreSQL.
select eng.id,
case et.id
when 2 then cwd.rate::text
when 1 then cwd.amount::text else 'SCD' end terms
I need to put some text like 'WC' before cwd.rate. Its giving me syntax error. Any idea how do I do this.
Related
So I am trying to figure out how to fail a SQL statement incase the case - when statement is not met.
I have been doing some searching and didn't find anything useful
this is the query for example ..
select
case when 1 = 1 then 'ok'
else < what to write here?>
end
If I am writing a typo or something like that this will fail the query entirely for syntax even if the condition is met .
Hope I could use your help !
I just tried running the following SQL query in databricks spark:
SELECT
uid_1,
uid_2,
CASE WHEN like IS TRUE THEN 1 ELSE 0 AS liked
FROM relevant_table
I run into an error message because "like" is also a SQL term. When working in redshift, one can simply put "like" in double quotes to resolve this issue, but that doesn't work with databricks spark. What's the fix here?
I'm not too familiar with spark, but maybe escaping with backticks does the trick:
SELECT ds,
st,
uid,
other_id,
CASE WHEN `like` IS TRUE OR super_like IS TRUE THEN 1 ELSE 0 AS liked,
s_number
FROM tinder_events.recs_rate_20228
I don't think Databricks needs special treatment of your column named "like". You could double-quote it or use backticks, but I don't believe it should matter.
The problem with the query is that it's a missing the END to the case expression. I believe that will resolve your error.
SELECT ds,
st,
uid,
other_id,
CASE WHEN like IS TRUE OR super_like IS TRUE THEN 1 ELSE 0 END AS liked,
s_number
FROM tinder_events.recs_rate_20228
I tried to use CASE/WHEN inside Postgresql to check two colums, but the results are odd.
As it's shown in the image below, all the lines where "gain_value" is 8 AND "patrimony_value" have a higher value return a wrong result.
This is my statement:
select stop_value, gain_value, patrimony_value,
case
when patrimony_value >= gain_value then 1
else 2
end
from copy.copy_stop_gain csg
Since it's a pretty straightforwad "if/else", i'm really not sure what i could be doing wrong.
Can anyone show me where is my mistake?
Try casting string values to numbers (or perhaps change column type in schema)...
select stop_value, gain_value, patrimony_value,
case
when patrimony_value::INTEGER >= gain_value::INTEGER then 1
else 2
end
from copy.copy_stop_gain csg
Trying to use a Case Statement for the first time, which is why the code is so small/simple. Returning a syntax error, supposedly with my table name [Impact].
Select Case [Impact]
Case Is = 0
[New Impact] = "1"
End Select
Any assistance is appreciated. I've looked around for solutions, but most of the time answers are related to something else in their code, and not anything I have in this small test code.
In MS Access, the logic one would normally used is:
Select iif([Impact] = 0, "1", NULL) as [New Impact]
You could use switch() as well, but that seems like overkill.
The Microsoft Access Case statement can only be used in VBA code.
The structure is like so:
Select Case test_expression
Case condition_1
result_1
Case condition_n
result_n
[Case Else
result_else]
End Select
My guess, is you missed the part about using this through VBA only.
I am trying to write a query in Hive with a Case statement in which the condition depends on one of the values in the current row (whether or not it is equal to its predecessor). I want to evaluate it on the fly, this way, therefore requiring a nested query, not by making it another column first and comparing 2 columns. (I was able to do the latter, but that's really second-best). Does anyone know how to make this work?
Thanks.
My query:
SELECT * ,
CASE
WHEN
(SELECT lag(field_with_duplicates,1) over (order by field_with_duplicates) FROM my_table b
WHERE b.id=a.id) = a.field_with_duplicates
THEN “Duplicate”
ELSE “”
END as Duplicate_Indicator
FROM my_table a
Error:
java.sql.SQLException: org.apache.spark.sql.AnalysisException: cannot recognize input near 'SELECT' 'lag' '(' in expression specification; line 4 pos 9
Notes:
The reason I needed the complicated 'lag' function is that the unique Id's in the table are not consecutive, but I don't think that's where it's at: I tested by substituting another simpler inner query and got the same error message.
Speaking of 'duplicates', I did search on this issue before posting, but the only SELECT's inside CASE's I found were in the THEN statement, and if that works the same, it suggests mine should work too.
You do not need the subquery inside CASE:
SELECT a.* ,
CASE
WHEN prev_field_with_duplicates = field_with_duplicates
THEN “Duplicate”
ELSE “”
END as Duplicate_Indicator
FROM (select a.*,
lag(field_with_duplicates,1) over (order by field_with_duplicates) as prev_field_with_duplicates
from my_table a
)a
or even you can use lag() inside CASE instead without subquery at all (I'm not sure if it will work in all Hive versions ):
CASE
WHEN lag(field_with_duplicates,1) over (order by field_with_duplicates) = field_with_duplicates
THEN “Duplicate”
ELSE “”
END as Duplicate_Indicator
Thanks to #MatBailie for the answer in his comment. Don't I feel silly...
Resolved