Nested case expression gives error in sql query - sql

What is wrong in this Oracle query. I am trying a nested case expression, but cant find the reason why the query fails.
SELECT USER_ID,
ADVISER_FORENAME,
ADVISER_SURNAME,
AGENT_CODE,
'ABC#WIPRO.COM' AS EMAIL_ADDRESS,
SUBMISSION_DATE,
STATUS_CHANGED_DATE,
CASE
WHEN (
CASE
WHEN TO_CHAR(TRUNC(status_changed_date), 'DY') IN ('fri','sat','sun')
THEN ((TRUNC(STATUS_CHANGED_DATE) - (TRUNC(SUBMISSION_DATE)))>4)
ELSE ((TRUNC(STATUS_CHANGED_DATE) - (TRUNC(SUBMISSION_DATE)))>6)
END)
THEN 'B'
ELSE 'A'
END AS CATEGORY_CODE,
PLAN_REF,
PRODUCT_CODE,
CASE
WHEN ((TRUNC(STATUS_CHANGED_DATE) - TRUNC(SUBMISSION_DATE))>4)
THEN 'IPP'
ELSE 'ICP'
END AS TOUCHPOINT_NAME
FROM PIPELINE_PLAN
WHERE STATUS =26
AND PRODUCT_TYPE =1
AND (TRUNC(STATUS_CHANGED_DATE) - TRUNC(SUBMISSION_DATE))<=4;
When i try running, it gives below error:
ORA-00907: missing right parenthesis
00907. 00000 - "missing right parenthesis"
*Cause:
*Action:
Error at Line: 53 Column: 84
I checked for missing parenthesis but found none.
Please help.

Your nested CASEs are a bit too clever. In oracle, comparisons (yielding a 3-way boolean result TRUE/FALSE/UNKNOWN) and expressions (yield other kinds of values) are not interchangeable.
The result of a CASE expression is not a boolean, so it can't be used as the argument to a WHEN in an outer CASE, which requires a boolean.
You have a structure like this in your query:
CASE
WHEN (
CASE
WHEN x IN ('1','2','3')
THEN (y>4)
ELSE (y>6)
END)
THEN 'B'
ELSE 'A'
END
which needs to be rewritten as
CASE
WHEN y > (CASE WHEN x IN ('1','2','3') THEN 4 ELSE 6 END)
THEN 'B'
ELSE 'A'
END
or
CASE
WHEN (x IN ('1','2','3') AND y>4) OR (x NOT IN ('1','2','3') AND y>6)
THEN 'B'
ELSE 'A'
END

I don't see a problem with the parentheses, but you cannot use booleans as results in the then clause of the case. Perhaps this version will work:
SELECT USER_ID, ADVISER_FORENAME, ADVISER_SURNAME, AGENT_CODE,
'ABC#WIPRO.COM' AS EMAIL_ADDRESS, SUBMISSION_DATE, STATUS_CHANGED_DATE,
(CASE WHEN (TRUNC(STATUS_CHANGED_DATE) - TRUNC(SUBMISSION_DATE) > 4 and
TO_CHAR(TRUNC(status_changed_date), 'DY') IN ('fri','sat','sun')
) or
(TRUNC(STATUS_CHANGED_DATE) - TRUNC(SUBMISSION_DATE) > 6 and
TO_CHAR(TRUNC(status_changed_date), 'DY') not IN ('fri','sat','sun')
)
THEN 'B'
ELSE 'A'
END) AS CATEGORY_CODE,
PLAN_REF, PRODUCT_CODE,
(CASE WHEN TRUNC(STATUS_CHANGED_DATE) - TRUNC(SUBMISSION_DATE) > 4
THEN 'IPP'
ELSE 'ICP'
END) AS TOUCHPOINT_NAME
FROM PIPELINE_PLAN
WHERE STATUS = 26 AND PRODUCT_TYPE = 1 AND
TRUNC(STATUS_CHANGED_DATE) - TRUNC(SUBMISSION_DATE) <= 4;
Also, based on the where clause, the condition TRUNC(STATUS_CHANGED_DATE) - TRUNC(SUBMISSION_DATE) > 6 can never be true.

Related

Why Would Unknown Column Be Referenced in SQL Query

I am in the process of updating some SQL queries to run against MariaDB instead of via SQL Anywhere. One query I'm running is erroring with this:
Error Code: 1054. Unknown column 'choice' in 'field list'
That is for this query:
SELECT
(select firstname||' '||lastname||' ('||service||')' from staff_members where id_number = customer_assignment_reviews.staff_member_id) as Rep,
(select customer_firstname||' '|| customer_lastname from customers where id_number = customer_assignment_reviews.cs_id) as Cus,
last_modified as "Response Date",replace(review_reason,'’','') as "Reason",
(Select choice = CASE
when accepted = 0 then 'No'
when accepted = 1 then 'Yes'
end) as "Accepted?"
FROM customer_assignment_reviews
where staff_member_id in (Select id_number from kar.staff_members where division_id = 6)
and "Response Date" between today() - 7 and today() /* Date Range */
and "Accepted?" = 'No'
Order by 3 desc
Is this error message as straightforward as it sounds? It's simply saying the column "choice" doesn't exist on the target table?
I'm just trying to reason through why this code (which I inherited) would be referencing a column that does not exist. Could something be expected here at runtime?
You don't need to use subquery in SELECT list
SELECT
-- ...
(Select choice = CASE
when accepted = 0 then 'No'
when accepted = 1 then 'Yes'
end) as "Accepted?"
=>
SELECT
CASE
when accepted = 0 then 'No'
when accepted = 1 then 'Yes'
end as "Accepted?"
Additionaly syntax SELECT alias = expression is only T-SQL specific:
SELECT alias = 1
<=>
SELECT 1 AS alias
What is this supposed to mean?
(Select choice = CASE
when accepted = 0 then 'No'
when accepted = 1 then 'Yes'
end) as "Accepted?"
Very importantly, a select is not needed here. You might mean:
(case when accepted = 0 then 'No'
when accepted = 1 then 'Yes'
end) as is_accepted -- prefer to not have to need escape characters
If accepted only takes those two values, you can simplify this to:
elt(accepted + 1, 'No', 'Yes') as is_accepted

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.

Select Case Multiple When Select Conditions Gives Error

The code below works fine.
(SELECT (CASE
WHEN (PA.ACTOR_KIND = 5) THEN
(SELECT POP.KDS_TEKLIF_TUT_TL)
ELSE 0 END) AS Expr1) AS YKF_CUSTOMER_LIMIT
However,when I add another "when" condition it gives error.
(SELECT (CASE
WHEN (PA.ACTOR_KIND = 5) THEN
(SELECT POP.KDS_TEKLIF_TUT_TL)
WHEN (PA.ACTOR_KIND = 10) THEN
(SELECT POP.KDS_ALICI_LIM_TL)
ELSE 0 END) AS Expr1) AS YKF_CUSTOMER_LIMIT
It seems all logical to me but not for sql
**Edit It was caused by typo error,there is nothing wrong with those statements.
I don't know if this will fix your problem, but you don't need SELECT for the WHEN clauses:
SELECT (CASE WHEN PA.ACTOR_KIND = 5
THEN POP.KDS_TEKLIF_TUT_TL
WHEN PA.ACTOR_KIND = 10
THEN POP.KDS_ALICI_LIM_TL
ELSE 0
END) AS YKF_CUSTOMER_LIMIT
You don't need to give the expression a name in the subquery as well as in the outer query. One name is enough. (Although that doesn't cause your error.)

How to use Case-When in another Case-When Condition in Oracle?

when execute the following SQL in Oracle
SELECT (CASE
WHEN (CASE
WHEN (1=1)
THEN (1=1)
ELSE (1=0)
END)
THEN 1
ELSE 0
END) "m1"
FROM "mock_table_1" "t0";
it will throw the following error message
ORA-00907: missing right parenthesis
So what should I do if I want to use Case-When in another Case-When Condition?
(You can omit the parentheses ) FROM DUAL is just for test
SELECT CASE WHEN CASE
WHEN 1=1 THEN 1
ELSE 0
END =1 THEN 1
ELSE 0 END "m1"
FROM DUAL;
CASE returns a value. So after END you can do your next condition

SQL Statement Case When With Select * Syntax

I have a SQL Statement as below:
SELECT * ,(CASE WHEN DRV_ID IS NOT NULL THEN CASE WHEN (DRV_ID)<'100' thenconcat(rental_type_c,'*')ENDELSE '9' end) as testing99FROM dmtb_driver;
just wondering why it will prompt me error "From key world no found." If i change my statement in such way:
SELECT DRV_Name,DRV_ID ,(CASE WHEN DRV_ID IS NOT NULL THEN CASE WHEN (DRV_ID)<'100' thenconcat(rental_type_c,'*')ENDELSE '9' end) as testing99FROM dmtb_driver;
it working perfectly. Just curious isn't i need to type out all the field name 1 by 1 instead of using '*'?
You need to alias your table.
SELECT
d.*,
CASE WHEN DRV_ID IS NOT NULL THEN
CASE WHEN (DRV_ID)<'100' THEN
CONCAT(rental_type_c,'*')
END
ELSE
'9'
END as testing99
FROM
dmtb_driver d;
I think your logic could use some simplification too.
Ii think you missed space before from and concat, also I don't think so two case statement is required.
SELECT * ,(CASE WHEN DRV_ID IS NOT NULL AND (DRV_ID)<'100' then concat(rental_type_c,'*') ELSE '9' end) as testing99
FROM dmtb_driver;