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.
Related
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
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.
I've tried to follow this answer (to a different problem that what I have). However, I'm getting the error telling me that the syntax is incorrect near IF.
select if(1 < 2, 3, 4) as Reply
from Unit
The above is the exact syntax I'm using (the table Unit exists but has nothing to do with the values, of course. Sometimes I just wish to be able to manipulate some columns for better visibility when I play in SQL Studio.
Please note that I'm not asking about how to coalesce nor how to handle null values. If it appears like if I am, then either you've misunderstood the question or I haven't been clear with my explanation. Sorry about that in advance.
You are looking for the case clause:
select
case when 1 < 2
then 3
else 4 end as Reply
from Unit
Or if you only wish to see the result once (as opposed to for each row in the table):
select
case when 1 < 2 then 3 else 4 end as Reply
I believe this is the syntax you are looking for, it works from sqlserver 2012:
SELECT IIF( 1 < 2, 3, 4)
The syntax is:
IIF ( boolean_expression, true_value, false_value )
I'm writing my first SQL CASE statement and I have done some research on them. Obviously the actual practice is going to be a little different than what I read because of context and things of that nature. I understand HOW they work. I am just having trouble forming mine correctly. Below is my draft of the SQL statement where I am trying to return two values (Either a code value from version A and it's title or a code value from version B and its title). I've been told that you can't return two values in one CASE statment, but I can't figure out how to rewrite this SQL statement to give me all the values that I need. Is there a way to use a CASE within a CASE (as in a CASE statement for each column)?
P.S. When pasting the code I removed the aliases just to make it more concise for the post
SELECT
CASE
WHEN codeVersion = A THEN ACode, Title
ELSE BCode, Title
END
FROM Code.CodeRef
WHERE ACode=#useCode OR BCode=#useCode
A case statement can only return one value. You can easily write what you want as:
SELECT (CASE WHEN codeVersion = 'A' THEN ACode
ELSE BCode
END) as Code, Title
FROM Code.CodeRef
WHERE #useCode in (ACode, BCode);
A case statement can only return a single column. In your scenario, that's all that is needed, as title is used in either outcome:
SELECT
CASE
WHEN codeVersion = "A" THEN ACode,
ELSE BCode
END as Code,
Title
FROM Code.CodeRef
WHERE ACode=#useCode OR BCode=#useCode
If you actually did need to apply the case logic to more than one column, then you'd need to repeat it.
Here is what I normally use:
SELECT
CASE
WHEN codeVersion = "A" THEN 'ACode'
WHEN codeVersion = "B" THEN 'BCode'
ELSE 'Invalid Version'
END as 'Version',
Title
FROM Code.CodeRef
WHERE
CASE
WHEN codeVersion = "A" THEN ACode
WHEN codeVersion = "B" THEN BCode
ELSE 'Invalid Version'
END = 'Acode'
my suggestion uses an alias. note on aliases: unfortunately you can't use the alias 'Version' in a where/group by clause. You have to use the whole case statement again. I believe you can only use an alias in an Order By.
Using a Case Statement in a query seems to work fine as long as the only case you want to match to is a string. But if you want to put ">" greater than (date or number), etc, SSMS automatically adds apostrophes around your statement (below) as if it were a string.
How can I get it to accept "greater than date" or "greater than number", etc??
There are two types of CASE expression:
The simple CASE expression compares an expression to a set of simple expressions to determine the result.
The searched CASE expression evaluates a set of Boolean expressions to determine the result.
You want a searched CASE expression. This means that the WHEN should come immediately after the CASE keyword.
CASE WHEN SoldDate <= SubjectDate THEN ...
There are two types of CASE expressions (from msdn):
A Simple CASE compares the specified expression for EQUIVALENCY ONLY
SELECT FieldName CASE WHEN 'Blah' THEN 'Foo' ELSE 'Bah' END
A Searched CASE can do inequalities too but has more verbose syntax:
SELECT CASE WHEN FieldName >= 'Blah' THEN 'Foo' ELSE 'Bah' END
You are trying to use the Simple syntax and need to use the Searched syntax by explicitly writing out the fieldname and comparison for each evaluation.
Your CASE statement syntax is incorrect. WHEN should come directly after CASE
This:
CASE SoldDate WHEN ...
Should be this:
CASE WHEN SoldDate <= SubjectDate THEN SoldFor ELSE EstSalePrice END
Additional Information
Take a look at the MSDN docs for additional examples of CASE syntax.