Case when using IN clause - sql

I need a query to return a certain result for a certain column depending on what value the column that is being run the select statement against has.
if the column is one of the following : I, D, U then I want to return Y
if the column is one of the following : N, E, D then I want to return N
else : I want to return NULL
I wrote the following statement but it doesn't work.
SELECT HIERARCHY_TYPE,
NODE_ID,
NODE_TYPE,
NODE_NAME,
NODE_LEVEL,
PREFERRED_ALIAS,
PARENT_NODE_ID,
CASE ACTIVE_INDICATOR
WHEN ('I' or 'U' or 'Y') THEN 'Y'
WHEN ('D' or 'E' or 'N') THEN 'N'
ELSE NULL
END
FROM MV_HIERARCHY MV;
Is there a way to rewrite it without using multiple OR clauses for each possible value?

I'd use the IN operator:
SELECT HIERARCHY_TYPE,
NODE_ID,
NODE_TYPE,
NODE_NAME,
NODE_LEVEL,
PREFERRED_ALIAS,
PARENT_NODE_ID,
CASE
WHEN ACTIVE_INDICATOR IN ('I','U','Y') THEN 'Y'
WHEN ACTIVE_INDICATOR IN ('D','E','N') THEN 'N'
ELSE NULL
END AS ACTIVE_INDICATOR
FROM MV_HIERARCHY MV;

CASE
WHEN ACTIVE_INDICATOR IN ('I','U','Y') THEN 'Y'
WHEN ACTIVE_INDICATOR IN ('D', 'E', 'N') THEN 'N'
ELSE NULL -- useless, but for readbility
END as ACTIVE_INDICATOR
You've got to repeat ACTIVE_INDICATOR, cause I don't think (may be wrong) you can use the syntax
CASE <field>
WHEN IN()
but you can use
CASE
WHEN <field> IN()

decode(mod(nullif(instr('IDUEYN',active_indicator),0),2),0,'N',1,'Y')

Related

How can I use CASE clause twice in SQL Query

I am trying to apply two conditions in one SQL Query.
(select DISTINCT (
CASE WHEN (
ABC.GemUserID = '99' )
OR ABC.GemUserID != '99'
THEN 'Yes'
ELSE 'No'
END)) AS AllWell
This gives me output as "Yes" where as the CASE is true only for 1 file like below :
Current Result:
99 , Yes
99 , Yes
99 , Yes
Expected Result:
99 , No
99 , No
99 , Yes
I am using the below query but the SQL Query Intellisence is identifying it as wrong.
Wrong Query:
(select DISTINCT (
CASE WHEN ( ABC.GEMUserID = '99' THEN 'Yes' else 'No'
CASE WHEN ( ABC.GEMUserID != '99' THEN 'No' else 'Yes'
END)) AS AllWell
After fixing the above Wrong Query:
(select DISTINCT
(CASE WHEN ABC.GemUserID = '99' THEN 'Yes' else 'No' END),
(CASE WHEN ABC.GemUserID != '99' THEN 'No' else 'Yes' END))
AS AllWell
But I am getting error:
Msg 116, Level 16, State 1, Line 17 Only one expression can be
specified in the select list when the subquery is not introduced with
EXISTS.
How to fix this?
select distinct is -- itself -- part of SQL syntax. The distinct is not a function. It should not be followed by parentheses. So, if I understand your question:
select DISTINCT
( CASE WHEN ABC.GEMUserID = '99' THEN 'Yes' else 'No' END),
( CASE WHEN ABC.GEMUserID <> '99' THEN 'No' else 'Yes' END) as AllWell
Do you plan on giving the first column a name?
select DISTINCT
CASE WHEN ABC.GEMUserID = '99' THEN 'Yes'
ELSE 'No' -- This is automatically When ABC.GEMUserID <> '99'
END AS AllWell
According to the error, your query is a subquery (probably behind IN?) in a larger SQL command. Therefore, it is not possible for such subquery to return more than one column.
So your first query, you've said:
CASE WHEN userID = 99 OR userID != 99
In other words:
CASE WHEN 1=1
This is why it returns yes for everything (not sure what the difference between your current and expected result should be considering that the userID is 99 for all rows).
For your erroneous query, seems you're returning that select in the middle of another select (since you alias it at the end). Due to that, you cannot return more than one column in your nested select. You do not need the second CASE statement, simply change your query to:
(select DISTINCT
CASE WHEN ABC.GemUserID = '99' THEN 'Yes' Else 'No' End) AS AllWell
Assuming that you hold the missing pieces to the query such as the FROM.

Oracle: CASE conditional STATEMENT in the WHERE Clause is not working?

I have below query with CASE statement and this is trowing "missing keyword error"
Can you please help.
select *
from podConfigKey_Tab PCK
WHERE
CASE WHEN (PCK.Keyid = 'TLMAPIConfigMgr.UseDB'
and PCK.DEFAULTKEYIDVALUE = 'FALSE')
THEN PCK.Keyid = 'TLMAPIConfigMgr.UseDB'
ELSE PCK.Keyid != 'TLMAPIConfigMgr.UseDB'
END;
A case expression returns a single value, not a syntactic construct like a=b. You could, however, emulate this behavior with a series of logical operators:
SELECT *
FROM podConfigKey_Tab PCK
WHERE PCK.DEFAULTKEYIDVALUE = 'FALSE' OR
PCK.Keyid != 'TLMAPIConfigMgr.UseDB'
your query should be something more like as mentioned below (removed the else portion to make the below query work), you need to have predicate after the WHERE clause so that you can match value that is return by the CASE statment
select * from podConfigKey_Tab PCK
WHERE PCK.Keyid =
CASE WHEN (PCK.Keyid = 'TLMAPIConfigMgr.UseDB' and PCK.DEFAULTKEYIDVALUE = 'FALSE') THEN 'TLMAPIConfigMgr.UseDB'
END ;
The Oracle CASE expression (like DECODE) returns a value, but by itself it is not a predicate which can evaluate to TRUE or FALSE. You need to establish some condition such that the value returned by the CASE statement can be evaluated. For example:
with sample_data as
(select 'dog' pet, 'y' has_fur from dual union all
select 'cat', 'y' from dual union all
select 'bird', 'n' from dual)
select *
from sample_data
where (case when has_fur = 'y' then 1 else 0 end) = 1;
SQL Fiddle Example

multiple values to oracle case statement then

Can some one please explain how to pass multiple values to oracle case statement Then
SELECT *
FROM impl_debitor_information
WHERE soft_delete='F'
AND SHOP_ID ='4987bc1b-c0a8-6cb7-12f4-0243011f7099'
AND (debitor_type IS NULL
OR debitor_type IN (CASE
WHEN (SELECT techfund_debitor_enabled
FROM impl_shop
WHERE shop_id='4987bc1b-c0a8-6cb7-12f4-0243011f7099') = 'YES' THEN ('T','D')
ELSE 'D'
END))
If values from
select techfund_debitor_enabled from impl_shop where shop_id='4987bc1b-c0a8-6cb7-12f4-0243011f7099' is "YES" then I need to pass 2 values to in clause, if not single value
Thanks in advance
CASE will only return a single value. You must rewrite your query. Something like this:
SELECT *
FROM impl_debitor_information i, impl_shop where shop_id s
WHERE d.soft_delete='F'
AND d.shop_id ='4987bc1b-c0a8-6cb7-12f4-0243011f7099'
AND d.shop_id = s.shop_id
AND (d.debitor_type IS NULL
OR (d.debitor_type IN ('T','D') AND s.techfund_debitor_enabled = 'YES')
OR (d.debitor_type IN ('D') AND s.techfund_debitor_enabled <> 'YES'))
There might be errors in it, I didn't test the query.

PostgreSQL, SELECT CASE COALESCE

I need to 'name' categories: mycat is a text column with possible values '0' to '4'.
SELECT CASE mycat
WHEN '0' THEN 'ZERO'
WHEN '1' THEN 'ONE'
WHEN '2' THEN 'TWO'
WHEN '3' THEN 'THREE'
WHEN '4' THEN 'OTHER'
END AS my_category,
COALESCE(SUM(col1), 0),
COALESCE(SUM(col2), 0),
COALESCE(SUM(col3), 0)
FROM mytable
GROUP BY mycat
ORDER BY mycat;
That works OK, but I have some an error in my program which very rarely writes null (or '' as I can see in pgAdmin). In such cases I have to treat that '' the same as '0'.
But I can't get that!
I try like this:
SELECT CASE COALESCE(mycat, '0')
But this doesn't solve it at all.
How to get that '' will be summed and grouped together with '0' category?
PostgreSQL 9.3, Windows.
you need to use COALESCE in the group by and order by also similar to how you planned to change the case expression, but postgres is giving error , so another option is to wrap your statement in a subquery and do group by
SELECT my_category,
COALESCE(SUM(col1), 0),
COALESCE(SUM(col2), 0),
COALESCE(SUM(col3), 0)
FROM
(
SELECT CASE coalesce(mycat ,'0')
WHEN '0' THEN 'ZERO'
WHEN '1' THEN 'ONE'
WHEN '2' THEN 'TWO'
WHEN '3' THEN 'THREE'
WHEN '4' THEN 'OTHER'
WHEN '' THEN 'ZERO'
END AS my_category,
col1,
col2,
col3
FROM mytable
) T
GROUP BY my_category
ORDER BY my_category
You can have this without subquery. You could repeat the expression in the GROUP BY and ORDER BY clause. But it's much simpler to use the ordinal number of the output column instead:
SELECT CASE mycat
WHEN '1' THEN 'ONE'
WHEN '2' THEN 'TWO'
WHEN '3' THEN 'THREE'
WHEN '4' THEN 'OTHER'
ELSE 'ZERO' -- catches all other values
END AS my_category
, COALESCE(SUM(col1), 0) AS sum1
, COALESCE(SUM(col2), 0) AS sum2
, COALESCE(SUM(col3), 0) AS sum3
FROM mytable
GROUP BY 1
ORDER BY 1;
I chose the simplest and fastest code. The ELSE branch catches 0, '' and NULL - or any other value not yet filtered! But you say there are no others.
A couple of rants:
mycat is 'text' column with possible values '0' to '4'.
This is wrong in two ways.
Obviously, there are empty strings ('') and / or NULL values, too.
With that fixed, integer, smallint, of "char" with a CHECK cnstraint would be sensible choices for the data type. (Maybe even enum.) text, not so much.
To find out your actual range of values:
SELECT mycat, count(*) AS ct
FROM mytable
GROUP BY 1
ORDER BY 2 DESC;
If your client obfuscates NULL and empty values, test with mycat IS NULL. You need to know and understand the difference in many situations.
This orders by the resulting text in my_category like: ONE, OTHER, THREE, TWO, ZERO? I doubt you want that.

SQL case with different fields to check from same table

I have the following problem:
I have a select statement that includes a case part. Up til there it is easy the problem is that the case includes a check against another field in the same table.
select h.id,
case h.value
when 'P' then 'test'
when '' then 'failed'
when 'D' then 'passed'
else null end
as info,
b.text,
case h.diag
when h.value = '' [or 'failed' not sure tried both and didn't work]
else h.diag end
as diag1, h.date from valuetab h, texttab b where h.id=b.id
I want to have h.diag only to show values when h.value is not failed.
I always get the mistake that the = should be concat.. but that doesn't make sense in my eyes.
Any ideas??
Thats for all your help.
You can also write a case statement with your expression in a different place i.e.
SELECT CASE WHEN X = 1 THEN 'Y' WHEN X = 2 THEN 'Z'
I think what you want to do is something more like this:
SELECT CASE WHEN h.value = '' THEN h.diag end
Use the other form of case statement, which doesn't specify a column you want to look at:
select case
when column1 = 2 then 'Foo'
when other_column = 'blah' then 'Bar'
end
from table
The problem with using case column1 when... is that it implicitly compares column1 to each when clause. You can't then include a comparison to some other column in it.
You are missing a THEN portion of the WHEN clause, and specifying a condition where you could specify a value:
case h.value
when '' THEN NULL
else h.diag end
Ok got it....
after the 2nd case the "h.diag" must be removed....
so it is
case
when h.value = '' then null
else h.diag end
as diag1,