Nested Case expression
How many levels can i nest my CASE expression in a Update or any DML statement?
Hi,
Columns can have aliases; expressions that are just part of computing a column can't have aliases.
If you could assign an alias, Oracle doesn't provide any way to reference it.
If you need to reference an expression, make it a separate column, perhaps in a sub-query.
Similar Messages
-
Nested CASE expressions in SQL Server 2008 R2
Hi, For some reason when I use the below CASE expression in my SELECT statement I get duplicate records.
CASE WHEN <Condition 1> THEN <Expression 1>
WHEN <Condition 2> THEN <Expression 2>
WHEN <Condition 3> THEN <Expression 3>
WHEN <Condition 4> THEN <Expression 4>
END AS CaseResult
Also tried using coalesce with no luck. How to write a nested CASE expression which would yield just one expression. In case if I have to combine 2 different Expressions for 1 record, Is there a way to merge the 2 Expressions writing CASE
expression something like below.
STUFF ((SELECT ',' + CASE WHEN <Condition 1> THEN <Expression 1>
WHEN <Condition 2> THEN <Expression 2>
WHEN <Condition 3> THEN <Expression 3>
WHEN <Condition 4> THEN <Expression 4>
END
FOR XML PATH(''), ROOT('MyString'), TYPE).value('/MyString[1]','VARCHAR(MAX)'), 1, 1,'') AS CaseResult
--ResultSet
CaseResult
<Expression 1, Expression 2>
I am using SQL Server 2008 R2. Thanks in advance.......
IoneHi, For some reason when I use the below CASE expression in my SELECT statement I get duplicate records. Also tried using coalesce with no luck. How to write a nested CASE expression which would yeild just one expression. In case if I have to combine 2 different
Expressions for 1 record, Is there a way to merge the 2 Expressions writing CASE expression something like below in SQL Server 2008 R2.
CASE WHEN <Condition 1> THEN <Expression 1>
WHEN <Condition 2> THEN <Expression 2>
WHEN <Condition 3> THEN <Expression 3>
WHEN <Condition 4> THEN <Expression 4>
END AS CaseResultSTUFF ((SELECT ',' + CASE WHEN <Condition 1> THEN <Expression 1>
WHEN <Condition 2> THEN <Expression 2>
WHEN <Condition 3> THEN <Expression 3>
WHEN <Condition 4> THEN <Expression 4>
END
FOR XML PATH(''), ROOT('MyString'), TYPE).value('/MyString[1]'<span clas
Ione -
CASE STATEMENTS AND CASE EXPRESSIONS IN ORACLE9I PL/SQL
제품 : PL/SQL
작성날짜 : 2001-11-13
CASE STATEMENTS AND CASE EXPRESSIONS IN ORACLE9I PL/SQL
=======================================================
PURPOSE
아래의 자료는 Case 문에서 oracle 8.1.7과 Oracle 9i의 New Feature로 8.1.7에서는
sqlplus 에서만 가능했고, 9i 부터는 pl/sql 까지 가능하다.
Explanation
1. Oracle 8.1.7 Feature
Oracle 8.1.7 에서 Case 문은 Decode 문과 유사하지만, 기존의 decode 문을 쓰는 것보다
더 많은 확장성과 Logical Power와 좋은 성능을 제공한다. 주로 나이와 같이 category 별로
나눌때 주로 사용하고 Syntex는 아래와 같다.
CASE WHEN <cond1> THEN <v1> WHEN <cond2> THEN <v2> ... [ELSE <vn+1> ] END
각각의 WHEN...THEN 절의 argument 는 255 까지 가능하고 이 Limit를 해결하려면
Oracle 8i Reference를 참조하면 된다.
The maximum number of arguments in a CASE expression is 255, and each
WHEN ... THEN pair counts as two arguments. To avoid exceeding the limit of 128 choices,
you can nest CASE expressions. That is expr1 can itself be a CASE expression.
Case Example : 한 회사의 모든 종업원의 평균 봉급을 계산하는데 봉급이 $2000보다 작은경우
2000으로 계산을 하는 방법이 pl/sql을 대신하여 case function을 사용할 수 있다.
SELECT AVG(CASE when e.sal > 2000 THEN e.sal ELSE 2000 end) FROM emp e;
Case Example : 나이를 column으로 가지고 있는 customer table을 예로 들어보자.
SQL> SELECT
2 SUM(CASE WHEN age BETWEEN 70 AND 79 THEN 1 ELSE 0 END) as "70-79",
3 SUM(CASE WHEN age BETWEEN 80 AND 89 THEN 1 ELSE 0 END) as "80-89",
4 SUM(CASE WHEN age BETWEEN 90 AND 99 THEN 1 ELSE 0 END) as "90-99",
5 SUM(CASE WHEN age > 99 THEN 1 ELSE 0 END) as "100+"
6 FROM customer;
70-79 80-89 90-99 100+
4 2 3 1
1 SELECT
2 (CASE WHEN age BETWEEN 70 AND 79 THEN '70-79'
3 WHEN age BETWEEN 80 and 89 THEN '80-89'
4 WHEN age BETWEEN 90 and 99 THEN '90-99'
5 WHEN age > 99 THEN '100+' END) as age_group,
6 COUNT(*) as age_count
7 FROM customer
8 GROUP BY
9 (CASE WHEN age BETWEEN 70 AND 79 THEN '70-79'
10 WHEN age BETWEEN 80 and 89 THEN '80-89'
11 WHEN age BETWEEN 90 and 99 THEN '90-99'
12* WHEN age > 99 THEN '100+' END)
SQL> /
AGE_G AGE_COUNT
100+ 1
70-79 4
80-89 2
90-99 3
Example
2. Oracle 9i Feature
Oracle 9i부터는 pl/sql에서도 case문을 사용할 수 있으면 이것은
복잡한 if-else 구문을 없애고, C언어의 switch문과 같은 기능을 한다.
아래의 9i pl/sql Sample 및 제약 사항을 보면 아래와 같다.
Sample 1:
A simple example demonstrating the proper syntax for a case
statement
using a character variable as the selector. See the section entitled
'Restrictions' at the end of this article for details on which PLSQL
datatypes may appear as a selector in a case statement or
expression.
- - - - - - - - - - - - - - - - Code begins here - - - - - - - - - - - -
set serveroutput on
declare
achar char(1) := '&achar';
begin
case achar
when 'A' then dbms_output.put_line('The description was Excellent');
when 'B' then dbms_output.put_line('The description was Very Good');
when 'C' then dbms_output.put_line('The description was Good');
when 'D' then dbms_output.put_line('The description was Fair');
when 'F' then dbms_output.put_line('The description was Poor');
else dbms_output.put_line('The description was No such Grade');
end case;
end;
- - - - - - - - - - - - - - - - Code ends here - - - - - - - - - - - -
Sample 2:
A simple example demonstrating the proper syntax for a case
expression
using a character variable as the selector. See the section entitled
'Restrictions' at the end of this article for details on which PLSQL
datatypes may appear as a selector in a case statement or
expression.
- - - - - - - - - - - - - - - - Code begins here - - - - - - - - - - - -
set serveroutput on
declare
achar char(1) := '&achar';
description varchar2(20);
begin
description :=
case achar
when 'A' then 'Excellent'
when 'B' then 'Very Good'
when 'C' then 'Good'
when 'D' then 'Fair'
when 'F' then 'Poor'
else 'No such grade'
end;
dbms_output.put_line('The description was ' || description);
end;
- - - - - - - - - - - - - - - - Code ends here - - - - - - - - - - - -
NOTE: The above simple samples demonstrate two subtle differences in the
syntax
required for case statements and expressions.
1) A case STATEMENT is terminated using the 'end case' keywords; a
case
EXPRESSION is terminated using only the 'end' keyword.
2) Each item in a case STATEMENT consists of one or more
statements, each
terminated by a semicolon. Each item in a case expression
consists of
exactly one expression, not terminated by a semicolon.
Sample 3:
Sample 1 demonstrates a simple case statement in which the selector
is
compared for equality with each item in the case statement body.
PL/SQL
also provides a 'searched' case statement as an alternative; rather
than
providing a selector and a list of values, each item in the body of
the
case statement provides its own predicate. This predicate can be any
valid boolean expression, but only one case will be selected.
- - - - - - - - - - - - - - - - Code begins here - - - - - - - - - - - -
set serveroutput on
declare
achar char(1) := '&achar';
begin
case
when achar = 'A' then dbms_output.put_line('The description was
Excellent');
when achar = 'B' then dbms_output.put_line('The description was Very
Good');
when achar = 'C' then dbms_output.put_line('The description was
Good');
when achar = 'D' then dbms_output.put_line('The description was
Fair');
when achar = 'F' then dbms_output.put_line('The description was
Poor');
else dbms_output.put_line('The description was No such Grade');
end case;
end;
- - - - - - - - - - - - - - - - Code ends here - - - - - - - - - - - -
Sample 4:
This sample demonstrates the proper syntax for a case expression of
the
type discussed in Sample 3 above.
- - - - - - - - - - - - - - - - Code begins here - - - - - - - - - - - -
set serveroutput on
declare
achar char(1) := '&achar';
description varchar2(20);
begin
description :=
case
when achar = 'A' then 'Excellent'
when achar = 'B' then 'Very Good'
when achar = 'C' then 'Good'
when achar = 'D' then 'Fair'
when achar = 'F' then 'Poor'
else 'No such grade'
end;
dbms_output.put_line('The description was ' || description);
end;
- - - - - - - - - - - - - - - - Code ends here - - - - - - - - - - - -
Sample 5:
This sample demonstrates the use of nested case statements. It is
also
permissable to nest case expressions within a case statement (though
it
is not demonstrated here), but nesting of case statements within a
case
expression is not possible since statements do not return any value.
- - - - - - - - - - - - - - - - Code begins here - - - - - - - - - - - -
set serveroutput on
declare
anum1 number := &anum1;
anum2 number := &anum2;
answer number;
begin
case anum1
when 1 then case anum2
when 1 then answer := 10;
when 2 then answer := 20;
when 3 then answer := 30;
else answer := 999;
end case;
when 2 then case anum2
when 1 then answer := 15;
when 2 then answer := 25;
when 3 then answer := 35;
else answer := 777;
end case;
else answer := 555;
end case;
dbms_output.put_line('The answer is ' || answer);
end;
- - - - - - - - - - - - - - - - Code ends here - - - - - - - - - - - -
Sample 6:
This sample demonstrates nesting of case expressions within another
case
expression. Note again the absence of semicolons to terminate both
the
nested case expression and the individual cases of those
expressions.
- - - - - - - - - - - - - - - - Code begins here - - - - - - - - - - - -
set serveroutput on
declare
anum1 number := &anum1;
anum2 number := &anum2;
answer number;
begin
answer :=
case anum1
when 1 then case anum2
when 1 then 10
when 2 then 20
when 3 then 30
else 999
end
when 2 then case anum2
when 1 then 15
when 2 then 25
when 3 then 35
else 777
end
else 555
end;
dbms_output.put_line('The answer is ' || answer);
end;
- - - - - - - - - - - - - - - - Code ends here - - - - - - - - - - - -
Although PL/SQL anonymous blocks have been used in all of the examples
so far,
case statements and expressions can also be used in procedures,
functions, and
packages with no changes to the syntax.
The following samples are included for completeness and demonstrate the
use of
case statements and/or expressions in each of these scenarios.
Sample 7:
This sample demonstrates use of a case statement in a stored
procedure.
Note that this sample also demonstrates that it is possible for each
of
the items in the case body to consist of more than one statement.
- - - - - - - - - - - - - - - - Code begins here - - - - - - - - - - - -
set serveroutput on
create or replace procedure testcasestmt ( anum IN number ) is
begin
case
when anum = 1 then dbms_output.put_line('The number was One');
dbms_output.put_line('In case 1');
when anum = 2 then dbms_output.put_line('The number was Two');
dbms_output.put_line('In case 2');
when anum = 3 then dbms_output.put_line('The number was Three');
dbms_output.put_line('In case 3');
when anum = 4 then dbms_output.put_line('The number was Four');
dbms_output.put_line('In case 4');
when anum = 5 then dbms_output.put_line('The number was Five');
dbms_output.put_line('In case 5');
else dbms_output.put_line('The description was Invalid input');
dbms_output.put_line('In the else case');
end case;
end;
exec testcasestmt(&anum);
- - - - - - - - - - - - - - - - Code ends here - - - - - - - - - - - -
Sample 8:
This sample demonstrates the use of a case statement in a stored
package.
- - - - - - - - - - - - - - - - Code begins here - - - - - - - - - - - -
set serveroutput on
create or replace package testpkg2 is
procedure testcasestmt ( anum IN number );
function testcasestmt_f ( anum IN number ) return number;
end testpkg2;
create or replace package body testpkg2 is
procedure testcasestmt ( anum IN number ) is
begin
case
when anum = 1 then dbms_output.put_line('The number was One');
dbms_output.put_line('In case 1');
when anum = 2 then dbms_output.put_line('The number was Two');
dbms_output.put_line('In case 2');
when anum = 3 then dbms_output.put_line('The number was Three');
dbms_output.put_line('In case 3');
when anum = 4 then dbms_output.put_line('The number was Four');
dbms_output.put_line('In case 4');
when anum = 5 then dbms_output.put_line('The number was Five');
dbms_output.put_line('In case 5');
else dbms_output.put_line('The description was Invalid input');
dbms_output.put_line('In the else case');
end case;
end;
function testcasestmt_f ( anum IN number ) return number is
begin
case
when anum = 1 then dbms_output.put_line('The number was One');
dbms_output.put_line('In case 1');
when anum = 2 then dbms_output.put_line('The number was Two');
dbms_output.put_line('In case 2');
when anum = 3 then dbms_output.put_line('The number was Three');
dbms_output.put_line('In case 3');
when anum = 4 then dbms_output.put_line('The number was Four');
dbms_output.put_line('In case 4');
when anum = 5 then dbms_output.put_line('The number was Five');
dbms_output.put_line('In case 5');
else dbms_output.put_line('The description was Invalid input');
dbms_output.put_line('In the else case');
end case;
return anum;
end;
end testpkg2;
exec testpkg2.testcasestmt(&anum);
variable numout number
exec :numout := testpkg2.testcasestmt_f(&anum);
print numout
- - - - - - - - - - - - - - - - Code ends here - - - - - - - - - - - -
Sample 9:
This sample demonstrates the use of a case expression in a stored
package.
- - - - - - - - - - - - - - - - Code begins here - - - - - - - - - - - -
set serveroutput on
create or replace package testpkg is
procedure testcase ( anum IN number );
function testcase_f ( anum IN number ) return number;
end testpkg;
create or replace package body testpkg is
procedure testcase ( anum IN number ) is
anumber number := anum;
anothernum number;
begin
anothernum :=
case
when anumber = 1 then anumber + 1
when anumber = 2 then anumber + 2
when anumber = 3 then anumber + 3
when anumber = 4 then anumber + 4
when anumber = 5 then anumber + 5
else 999
end;
dbms_output.put_line('The number was ' || anothernum);
end;
function testcase_f ( anum IN number ) return number is
anumber number := anum;
anothernum number;
begin
anothernum :=
case
when anumber = 1 then anumber + 1
when anumber = 2 then anumber + 2
when anumber = 3 then anumber + 3
when anumber = 4 then anumber + 4
when anumber = 5 then anumber + 5
else 999
end;
dbms_output.put_line('The number was ' || anothernum);
return anothernum;
end;
end testpkg;
variable numout number
exec testpkg.testcase(&anum);
exec :numout := testpkg.testcase_f(&anum);
print numout
- - - - - - - - - - - - - - - - Code ends here - - - - - - - - - - - -
제약 사항
다음의 databasetype은 case 문에서 지원되지 않는다.
BLOB
BFILE
VARRAY
Nested Table
PL/SQL Record
PL/SQL Version 2 tables (index by tables)
Object type (user-defined type)
All of these types except for object types face a similar restriction
even for if statements (i.e. they cannot be compared for equality directly) so this is unlikely to change for these types. Lack of support for object types is simply an implementation restriction which may be relaxed in future releases.
Reference Ducumment
Oracle 8.1.7 Manual
NOTE:131557.1I have done the following code but doesn't
like the statement of - "case(butNext)". What do you mean "doesn't like" -- did you get an error message?
I'm guessing it won't compile because you're trying to switch on a Button.
I tried something
like "g.fillOval(100,50,70,90, BorderLayout.NORTH)"...no that doesn't make sense. You only use BorderLayout.NORTH when you're adding components to a BorderLayout layout manager. An oval is not a component and fillOval isn't adding a component and Graphics is not a Panel or layout manager.
Would appreciate it if someone could tell me how to position
shapes using the graohic method. I think the problem is that you're confusing shapes with components. -
Sql 2008 nested case statement
I have a question about understanding nested case statements in sql server 2008:
The sql looks like the folloiwng:
select numberofcases
from inventory
where inventory_cnt > 1000
(when select top 1
from inventory
where inventory_cnt > 750
then numberofcases = 750 * 30
when select top 2
from inventory
where inventory_cnt > 975
then numberofcases = 975 * 35
when select top 3
from inventory
where inventory_cnt > 1025
then numberofcases = 1025 / 10
when select top 4
from inventory
where inventory_cnt > 1050
then numberofcases = 1050 / 5) c * 2
as casesused, select CustomerNumber from inventory
I would like you to explain the following:
1. There are 4 when statements. Will the logic hit each when statemnet or will the logic
stop once the first when statement is true?
2. Would you explain what the c* 2 means from the school listed above?Please post DDL, so that people do not have to guess what the keys, constraints, Declarative Referential Integrity, data types, etc. in your schema are. Learn how to follow ISO-11179 data element naming conventions and formatting rules. Temporal data should
use ISO-8601 formats. Code should be in Standard SQL as much as possible and not local dialect.
This is minimal polite behavior on SQL forums.
SELECT CASE
WHEN Inventory_cnt > 1050 THEN 1050 / 5
WHEN Inventory_cnt > 1025 THEN 1025 / 10
WHEN Inventory_cnt > 750 THEN 750 * 30
WHEN Inventory_cnt > 975 THEN 975 * 35
ELSE NULL END AS cases_used
FROM Inventory;
I would like you to explain the following:
>> 1. There are 4 when statements. Will the logic hit each when statement or will the logic stop once the first when statement is true? <<
This code is garbage, not SQL. CASE is an expression, not a statement. Expressions return a scalar value. Your are trying to do control flow! And the answer is that a CASE works this way
1) look at the THEN clauses and determine the data type to use
2) test each WHEN clause and execute the first one that tests TRUE in left to right order.
>> 2. Would you explain what the c* 2 means from the school listed above? <<
Syntax error and more garbage code.
--CELKO-- Books in Celko Series for Morgan-Kaufmann Publishing: Analytics and OLAP in SQL / Data and Databases: Concepts in Practice Data / Measurements and Standards in SQL SQL for Smarties / SQL Programming Style / SQL Puzzles and Answers / Thinking
in Sets / Trees and Hierarchies in SQL -
SEELCT ........FROM ............WHERE
SUB_CLASS_CD = (CASE
WHEN CUR_VAR.CLASS_CD = 11 THEN
(CASE WHEN CUR_VAR.SUB_CLASS_CD = 17
*CUR_VAR.SUB_CLASS_CD
ELSE NULL
END)
WHEN CUR_VAR.CLAS_CD = 60 AND CUR_VAR.SUB_CLASS_CD IN (20,42,62,83,88,89,91,92) THEN
CUR_VAR.SUB_CLASS_CD
WHEN CUR_VAR.CLASS_CD = 70 THEN
CASE WHEN CUR_VAR.SUB_CLASS_CD = 83
CUR_VAR.SUB_CLASS_CD
ELSE NULL
END
ELSE CUR_VAR.SUB_CLASS_CD
END)
This is showing error message i.e. keyword missing at * marked place.
Can any body suggest me?
thanks in advance.Hi,
As SBH said, you left out the THEN keyword. The same thing happened in the 2nd nested CASE.
Always indent your code to show the structure of long expressions like this:
SUB_CLASS_CD = ( CASE
WHEN CUR_VAR.CLASS_CD = 11
THEN ( CASE
WHEN CUR_VAR.SUB_CLASS_CD = 17
THEN CUR_VAR.SUB_CLASS_CD
ELSE NULL
END
WHEN CUR_VAR.CLAS_CD = 60 -- Not class_cd (with 2 s's)?
AND CUR_VAR.SUB_CLASS_CD IN (20,42,62,83,88,89,91,92)
THEN CUR_VAR.SUB_CLASS_CD
WHEN CUR_VAR.CLASS_CD = 70
THEN CASE
WHEN CUR_VAR.SUB_CLASS_CD = 83
THEN CUR_VAR.SUB_CLASS_CD -- THEN needed here, too
ELSE NULL
END
ELSE CUR_VAR.SUB_CLASS_CD
END
)You can get the same results without nested CASE:
sub_class_cd = CASE
WHEN class_cd = 11 AND sub_class_cd = 17 THEN sub_class_cd
WHEN class_cd = 11 THEN NULL
WHEN class_cd = 70 AND sub_class_cd = 83 THEN sub_class_cd
WHEN class_cd = 70 THEN NULL
ELSE sub_class_cd
ENDThe test for class_cd = 60 (or clas_cd = 60) wasn't doing anything, so I removed it. If class_cd = 60, but sub_class_cd is in the list of special values, then sub_class_cd is returned by the THEN clause in the middle. If class_cd = 60, but sub_class_cd is not in the list of special values, then sub_class_cd is returned by the THEN clause at the end. Either way, sub_class_cd is returned, so why not do it just once, at the end?
You can also get the same results without CASE:
WHERE ( class_cd = 11
AND sub_class_cd != 17
OR ( class_cd = 70
AND sub_class_cd != 83
OR class_cd NOT IN (11, 70) -
How to use the CASE Expression in Where Cluase?
Hi All,
I'm trying to use the CASE Expression in the Where Clause at some trigger on the Form?
I've tried this Code:
Declare
N Number;
begin
SELECT COUNT(E.EMP_SID)
INTO N
FROM EMPLOYEES E, RANKS R
WHERE CASE WHEN R.qualification_sid = 1104 AND E.rank_sid = 8 THEN
(TO_DATE(E.RANK_DATE, 'DD-MM-RR')+(365*M.spe_per)+1)
ELSE
(TO_DATE(E.RANK_DATE, 'DD-MM-RR')+(365*M.mili_yea_per)+1)
END
BETWEEN TO_DATE('01-07-2011', 'DD-MM-RR') AND TO_DATE('31-07-2011', 'DD-MM-RR');
END;
When I run this code as a normal query at any SQL editor it works successfully, But When I Compile it at some trigger on the Form it gives me this error:
Encountered the symbol "CASE" when expecting one of the following:
( - + mod ......
Heeey how to specify the previous code to be shown as code in the thread?
Note: I'm using Forms 6iOK I tried it and worked but for one condition:
WHERE DECODE (E.qualification_sid, 1104,
(TO_DATE(E.RANK_DATE, 'DD-MM-RR')+(365*M.spe_per)+1),
(TO_DATE(E.RANK_DATE, 'DD-MM-RR')+(365*M.mili_yea_per)+1))
BETWEEN TO_DATE('01-07-2011', 'DD-MM-RR') AND TO_DATE('31-07-2011', 'DD-MM-RR')
But how to put two conditions for the same Expression:
WHERE DECODE ((E.qualification_sid, 1104) AND (E.RANK_SID, 8),
(TO_DATE(E.RANK_DATE, 'DD-MM-RR')+(365*M.spe_per)+1),
(TO_DATE(E.RANK_DATE, 'DD-MM-RR')+(365*M.mili_yea_per)+1))
BETWEEN TO_DATE('01-07-2011', 'DD-MM-RR') AND TO_DATE('31-07-2011', 'DD-MM-RR')
The previous code gives me this error: missing right parenthesis -
How to create nested case when statement in OBIEE 11g?
Hi All,
I need to create a formula using nested case when statement. The formula to be created is below:
=If([AWRV]<0; "<0";
If([AWRV]=0; "0";
If([AWRV]<=15; ">0 and <=15";
If([AWRV]<=25; ">15 and <=25";
If([AWRV]<=50; ">25 and <=50";
If([AWRV]<=75; ">50 and <=75";
If([AWRV]<=100; ">75 and <=100";
If([AWRV]<=200; ">100 and <=200";
If([AWRV]<=500; ">200 and <=500";
If([AWRV]<=1000; ">500 and <=1000";
If([AWRV]<=5000; ">1000 and <=5000";
If([AWRV]<=10000; ">5000 and <=10000"; ">10000"))))))))))))
How to recreate using Nested case when? I tried in many different ways but it is displaying syntax error in obiee11g. This is very critical. Can anybody shed light on this issue pls?
Thanks in advance,
ThenmozhiHoney26 wrote:
Hi All,
I need to create a formula using nested case when statement. The formula to be created is below:
=If([AWRV]<0; "<0";
If([AWRV]=0; "0";
If([AWRV]<=15; ">0 and <=15";
If([AWRV]<=25; ">15 and <=25";
If([AWRV]<=50; ">25 and <=50";
If([AWRV]<=75; ">50 and <=75";
If([AWRV]<=100; ">75 and <=100";
If([AWRV]<=200; ">100 and <=200";
If([AWRV]<=500; ">200 and <=500";
If([AWRV]<=1000; ">500 and <=1000";
If([AWRV]<=5000; ">1000 and <=5000";
If([AWRV]<=10000; ">5000 and <=10000"; ">10000"))))))))))))
How to recreate using Nested case when? I tried in many different ways but it is displaying syntax error in obiee11g. This is very critical. Can anybody shed light on this issue pls?
Thanks in advance,
ThenmozhiTry the below:
CASE WHEN "Fact - Open Chargeback"."Sub Chbk Amt" < 0 THEN ' <0'
WHEN "Fact - Open Chargeback"."Sub Chbk Amt" = 0 THEN '0'
WHEN "Fact - Open Chargeback"."Sub Chbk Amt" BETWEEN 0 AND 15 THEN '>0 AND <=15'
END
Hope this helps. -
Case expression error with a select query in the condition
Consider the two query below:
1. The first one use a case expression :
select case when 3 > (select 1 from dual)*2 then 1 else 0 end from dual
2. The equivalent with a decode expression:
select
decode(sign(3-(select 1 from dual)*2),1,1,0) from dual
The first query return an ORA-00905 error but not the second one. It seems that the arithmetic operation (select 1 from dual)*2 cause the error. The folowing query doesn't return this error:
select case when 3 > (select 2 from dual) then 1 else 0 end from dual
I run the test with Oracle 8.1.7.3 Enterprise Edition. Is it a known bug ?
Thomasora8174>select case when 3 > (select 1 from dual)*2 then 1 else 0 end from dual;
select case when 3 > (select 1 from dual)*2 then 1 else 0 end from dual
ERROR at line 1:
ORA-00905: missing keyword
ora8174>select case when 3 > cast((select 1 from dual)*2 as number) then 1 else 0 end from dual;
CASEWHEN3>CAST((SELECT1FROMDUAL)*2ASNUMBER)THEN1ELSE0END
1
1 row selected.
ora8174>select case when 3 > to_number((select 1 from dual)*2) then 1 else 0 end from dual;
CASEWHEN3>TO_NUMBER((SELECT1FROMDUAL)*2)THEN1ELSE0END
1
1 row selected.
ora8174>@connect
Connected.
ora9204>select case when 3 > (select 1 from dual)*2 then 1 else 0 end from dual;
CASEWHEN3>(SELECT1FROMDUAL)*2THEN1ELSE0END
1
1 row selected. -
Nesting EL expressions in core tags
Hi all,
I have a situation where I require to use dymanic textboxes based on the certain criteria. To make this work I used strut's LazyValidatorActionForm. It works like a charm but when I want to write out certain form property using EL, it doesn't work well. I am not sure how to use it with <c:out />. For example:
This is the code I tried to display a link with a value in the dynamic form:
<c:forEach var="counter" begin="0" end="${numTextFields}" >
?methodToCall=removeElement&id=<c:out value="${request['FAEditorForm'].id_${counter}}" />">Remove</a>
</c:forEach>
Here, I am trying to get the Form from the request attribute and invoke the method for property 'id_?' where '?' can be some number depending on the counter in the loop. When executing this page, I am getting an exception:
javax.servlet.jsp.JspException: Can't insert page '/pages/findingaids/FACustomContainerEditor.jsp' : javax.servlet.jsp.JspException: The taglib validator rejected the page: "tag = 'out' / attribute = 'value': An error occurred while parsing custom action attribute "value" with value "${request['FAEditorForm'].id_${counter}}": Encountered "{", expected one of ["}", ".", ">", "gt", "<", "lt", "==", "eq", "<=", "le", ">=", "ge", "!=", "ne", "[", "+", "-", "*", "/", "div", "%", "mod", "and", "&&", "or", "||"], "
I also tried using <c:set /> but looks like the JSTL tags does not allow nesting EL ('{') inside another EL. Not sure how to solve this issue. Any help is appreciated.
Thanks,
SriniTo solve the nested EL expression issue, you use the square brackets notation.
This should be close to what you want.
<c:forEach var="counter" begin="0" end="${numTextFields}" >
<c:set var="cVar" value="id_${counter}"/>
<c:out value="${FAEditorForm[cVar]"/>
</c:forEach>However instead of building the parameter string manually, you should let the struts tag do it for you.
Seeing as you have two parameters, you have to pass it a map.
<jsp:useBean id="paramMap" class="java.util.HashMap" scope="page"/>
<c:set target="${paramMap}" property="methodToCall" value="removeElement"/>
<c:forEach var="counter" begin="0" end="${numTextFields}" >
<c:set var="cVar" value="id_${counter}"/>
<c:set target="${paramMap}" property="id" value="${cVar}"/>
<html:link page="/FAEditor.do" name="paramMap">Remove </html:link>
<br>...
</c:forEach>Cheers,
evnafets -
Oracle:how to use max() function in case expression
how to use max() function in case expression, Please explain with any example
Hope this helps and should be self explanatory
with t as
(select 1 col,100 col2 from dual union
select 2 ,100 from dual union
select 2 ,200 from dual union
select 3,100 from dual union
select 3,200 from dual )
select col, case when max(col2)=100 then 'with 100 range'
when max(col2)=200 then 'with 200 range' end from t group by col -
Having Trouble with nested Case Statements
Hi Folks,
I'm having trouble getting my head round nested case statements. For the life of me I cannot see what I'm missing here (unless my approach is all wrong).
Any help much appreciated.
Script:
set serveroutput on format wrapped
set feedback off
set linesize 150
DECLARE
/* Set supported version here */
ora_version VARCHAR2(4);
unsupp_version EXCEPTION;
/* Archive Log Info */
db_log_mode VARCHAR2(12);
BEGIN
SELECT SUBSTR(VERSION, 1, 4)
INTO ora_version
FROM v$instance;
SELECT log_mode
INTO db_log_mode
FROM v$database;
CASE
WHEN ora_version = '10.2' THEN
DECLARE
TYPE t_db IS RECORD(
dflsh VARCHAR2(3),
dcscn NUMBER);
v_db t_db;
BEGIN
CASE
WHEN db_log_mode = 'ARCHIVELOG' THEN
EXECUTE IMMEDIATE 'SELECT INITCAP(flashback_on), current_scn FROM v$database'
INTO v_db;
DBMS_OUTPUT.PUT_LINE(' Flashback On : ' || v_db.dflsh);
DBMS_OUTPUT.PUT_LINE(' Current SCN : ' || v_db.dcscn);
DBMS_OUTPUT.PUT_LINE(' Log Mode : ' || db_log_mode);
DBMS_OUTPUT.PUT_LINE(' Version : ' || ora_version);
END;
ELSE
DBMS_OUTPUT.PUT_LINE(' Log Mode : ' || db_log_mode);
DBMS_OUTPUT.PUT_LINE(' Version : ' || ora_version);
END CASE;
END;
WHEN ora_version = '9.2' THEN
DECLARE
TYPE t_db IS RECORD(
dcscn NUMBER);
v_db t_db;
BEGIN
CASE
WHEN db_log_mode = 'ARCHIVELOG' THEN
EXECUTE IMMEDIATE 'SELECT current_scn FROM v$database'
INTO v_db;
DBMS_OUTPUT.PUT_LINE(' Current SCN : ' || v_db.dcscn);
DBMS_OUTPUT.PUT_LINE(' Log Mode : ' || db_log_mode);
DBMS_OUTPUT.PUT_LINE(' Version : ' || ora_version);
END;
ELSE
DBMS_OUTPUT.PUT_LINE(' Log Mode : ' || db_log_mode);
DBMS_OUTPUT.PUT_LINE(' Version : ' || ora_version);
END CASE;
END;
ELSE
RAISE unsupp_version;
END CASE;
EXCEPTION
WHEN unsupp_version THEN
DBMS_OUTPUT.PUT_LINE('');
DBMS_OUTPUT.PUT_LINE(' Unsupported Version '||ora_version||' !');
DBMS_OUTPUT.PUT_LINE('');
END;
set linesize 80
set feedback on
set serveroutput off
Gives errors:
END;
ERROR at line 31:
ORA-06550: line 31, column 7:
PLS-00103: Encountered the symbol ";" when expecting one of the following:
case
ORA-06550: line 37, column 1:
PLS-00103: Encountered the symbol "WHEN"
ORA-06550: line 50, column 28:
PLS-00103: Encountered the symbol ";" when expecting one of the following:
case
Edited by: milkyjoe on 28-Apr-2010 05:38Hi,
Never write, much less post, unformatted code.
Indent the code to show the extent of multi-line structures like BEGIN and CASE.
For example:
DECLARE
/* Set supported version here */
ora_version VARCHAR2 (4);
unsupp_version EXCEPTION;
/* Archive Log Info */
db_log_mode VARCHAR2 (12);
BEGIN
SELECT SUBSTR(VERSION, 1, 4)
INTO ora_version
FROM v$instance;
SELECT log_mode
INTO db_log_mode
FROM v$database;
CASE
WHEN ora_version = '10.2' THEN
DECLARE
TYPE t_db IS RECORD(
dflsh VARCHAR2(3),
dcscn NUMBER);
v_db t_db;
BEGIN
CASE
WHEN db_log_mode = 'ARCHIVELOG' THEN
EXECUTE IMMEDIATE 'SELECT INITCAP(flashback_on), current_scn FROM v$database'
INTO v_db;
DBMS_OUTPUT.PUT_LINE(' Flashback On : ' || v_db.dflsh);
DBMS_OUTPUT.PUT_LINE(' Current SCN : ' || v_db.dcscn);
DBMS_OUTPUT.PUT_LINE(' Log Mode : ' || db_log_mode);
DBMS_OUTPUT.PUT_LINE(' Version : ' || ora_version);
END;
...The code above is what you posted, with some whitespace added.
The error is much clearer; the last CASE statement concludes with END, but CASE blocks always have to conclude with END CASE .
Why are you using a nested BEGIN block in the code above? Are you plannning to add an EXCEPTION handler later?
When posting formatted text on this site, type these 6 characters:
\(small letters only, inside curly brackets) before and after each section of formatted text, to preserve spacing. -
How to create nested CASE statements in PL/SQL
Can anyone please tell how to create Nested CASE statements in PL/SQL with proper syntax?
It would be better if you can help with an example.
Thank you!Something like this:
SQL> set serveroutput on
SQL> declare
2 v1 number := 2;
3 v2 varchar2(1) := 'C';
4 begin
5 case v1
6 when 1 then dbms_output.put_line('First');
7 when 2 then begin
8 case v2
9 when 'A' then dbms_output.put_line('Found A');
10 when 'B' then dbms_output.put_line('Found B');
11 when 'C' then dbms_output.put_line('Found C');
12 else dbms_output.put_line('NONE');
13 end case;
14 end;
15 else dbms_output.put_line('Else');
16 end case;
17 end;
18 /
Found C
PL/SQL procedure successfully completed
SQL> If you have further doubts regarding syntax you can read the docs on the Case statement here:
http://download.oracle.com/docs/cd/E11882_01/appdev.112/e10472/case_statement.htm -
Hi all,
I am creating a Java application that - among other useful things - needs to change the values of the DB according a to a huge (maybe > 1000 mappings) , user-created (through the UI) mapping like the one below:
DB Value UI Value
a --> A1
b --> B2
c --> C3
d --> D4
e --> E5
f --> F6
So, if my SELECT contains the column that needs its values changed (the values on the left), I need to somehow get the values on the right.
Normally, I would create a table with this mapping and use it in my query to have it return the values I need. However, I cannot do this, because I don't have (and will not get) privileges to create tables or procedures.
* One solution that comes into my mind is to generate through Java a query with a HUGE case expression, with a case for each mapping. But this does not seem right.
* Another thing I could do is make the query return the DB value and in my application I could do this mapping.
Is there a more elegant way to do the mapping without creating a table in the DB?
Thanks,
Markos
Edited by: user622271 on Jun 23, 2009 6:42 AMYour best bet is a lookup table. If you do not have privilege to create a lookup table for some reason, then you should be allowed to create a view.
I am not sure how many mappings you will finally have, but it sounds like too much to DECODE. But in a simpler circumstance, you could use DECODE directly in your application SQL or use it to create a view then access the view from your application instead of the table.
SELECT DECODE(db_value,
'a','A1',
'b','B2',
'c','C3',
'd','D4',
'x','Xn') ui_value
FROM table_name
/If you are lucky to have your db_value exist with some kind of pattern, then you can reduce the number of lines with some pattern matching. -
Obiee nested case when statements
Hi Obiee Experts, I need to retrieve amounts for 'last september' so i created a column using this case statement: case when MONTH(CURRENT_DATE) <= 9 then (YEAR(CURRENT_DATE) -1) * 100 + 9 END -- now i want to retrieve the amount for last september using another case statemnt but i get a syntax err message, when i use: CASE WHEN (case when MONTH (CURRENT_DATE) <= 9 then (YEAR(CURRENT_DATE) -1) * 100 + 9 END) THEN "Fact123"."Net Obligation Amount" END ---- Any ideas how to resolve? I will be forever grateful - cheers, Elena
Honey26 wrote:
Hi All,
I need to create a formula using nested case when statement. The formula to be created is below:
=If([AWRV]<0; "<0";
If([AWRV]=0; "0";
If([AWRV]<=15; ">0 and <=15";
If([AWRV]<=25; ">15 and <=25";
If([AWRV]<=50; ">25 and <=50";
If([AWRV]<=75; ">50 and <=75";
If([AWRV]<=100; ">75 and <=100";
If([AWRV]<=200; ">100 and <=200";
If([AWRV]<=500; ">200 and <=500";
If([AWRV]<=1000; ">500 and <=1000";
If([AWRV]<=5000; ">1000 and <=5000";
If([AWRV]<=10000; ">5000 and <=10000"; ">10000"))))))))))))
How to recreate using Nested case when? I tried in many different ways but it is displaying syntax error in obiee11g. This is very critical. Can anybody shed light on this issue pls?
Thanks in advance,
ThenmozhiTry the below:
CASE WHEN "Fact - Open Chargeback"."Sub Chbk Amt" < 0 THEN ' <0'
WHEN "Fact - Open Chargeback"."Sub Chbk Amt" = 0 THEN '0'
WHEN "Fact - Open Chargeback"."Sub Chbk Amt" BETWEEN 0 AND 15 THEN '>0 AND <=15'
END
Hope this helps. -
select count(*)
from vwsr_all_merged_data s
where sr_tio_priority in ('Level 0','CEO','TER','Priority Assistance','Enquiry')
and sr_status = 'Open'
and SR_BUS_UNIT IN ('Business Support and Improvement','Finance and Administration','Cust Sat Simplification and Productivity','Project New and Customer Experience','Corp Strategy and Customer Experience','Other')
COUNT(*)
126the same should be possible dynamically with Case expression ,before that i create the string as
SELECT
CASE
WHEN :name = 'BS&I' THEN ('('||''''||'Business Support and Improvement'||''''||','||''''||'Finance and Administration'||','||''''||'Cust Sat Simplification and Productivity'
||''''||','||''''||'Project New and Customer Experience'||''''||','||''''||'Corp Strategy and Customer Experience'||''''||','||''''||'Other'||''''||')')
ELSE null
END
FROM dual
CASEWHEN:NAME='BS&I'THEN('('||''''||'BUSINESSSUPPORTANDIMPROVEMENT'||''''||','||
('Business Support and Improvement','Finance and Administration,'Cust Sat Simplification and Productivity','Project New and Customer Experience','Corp Strategy and Customer Experience','Other')
SQL> var name varchar2(10)
SQL> exec :name:='BS&I'
PL/SQL procedure successfully completed.the same string query i put here within subquery to make above string , it does not work.
select count(*)--sr_bus_unit,count(trunc(sr_open_date))
from vwsr_all_merged_data s
where sr_tio_priority in ('Level 0','CEO','TER','Priority Assistance','Enquiry')
and sr_status = 'Open'
and SR_BUS_UNIT IN
( SELECT
CASE
WHEN :name = 'BS&I' THEN ('('||''''||'Business Support and Improvement'||''''||','||''''||'Finance and Administration'||','||''''||'Cust Sat Simplification and Productivity'
||''''||','||''''||'Project New and Customer Experience'||''''||','||''''||'Corp Strategy and Customer Experience'||''''||','||''''||'Other'||''''||')')
ELSE null
END
FROM dual
COUNT(*)--SR_BUS_UNIT,COUNT(TRUNC(SR_OPEN_DATE))
0Edited by: user13653962 on 24/01/2013 17:09
Edited by: user13653962 on 24/01/2013 17:10No man , it is still no working , anyhow i really appreciate yours help , yours help drive me to find other way and you contributed 70% to achieve the result. Next query worked.
SQL> SET DEFINE OFF;
SQL>
SQL> SELECT COUNT (*)
2 FROM vwsr_all_merged_data s
3 WHERE sr_tio_priority IN
4 ('Level 0', 'CEO', 'TER', 'Priority Assistance', 'Enquiry')
5 AND sr_status = 'Open'
6 AND SR_BUS_UNIT IN
7 ('Business Support and Improvement',
8 'Finance and Administration',
9 'Cust Sat Simplification and Productivity',
10 'Project New and Customer Experience',
11 'Corp Strategy and Customer Experience',
12 'Other')
13 UNION ALL
14 SELECT COUNT (*) --sr_bus_unit,count(trunc(sr_open_date))
15 FROM vwsr_all_merged_data s
16 WHERE sr_tio_priority IN
17 ('Level 0', 'CEO', 'TER', 'Priority Assistance', 'Enquiry')
18 AND sr_status = 'Open'
19 AND SR_BUS_UNIT IN
20 (SELECT CASE
21 WHEN 'BS&I' = 'BS&I'
22 THEN
23 ( ''''
24 || 'Business Support and Improvement'
25 || ''''
26 || ','
27 || ''''
28 || 'Finance and Administration'
29 || ''''
30 || ','
31 || ''''
32 || 'Cust Sat Simplification and Productivity'
33 || ''''
34 || ','
35 || ''''
36 || 'Project New and Customer Experience'
37 || ''''
38 || ','
39 || ''''
40 || 'Corp Strategy and Customer Experience'
41 || ''''
42 || ','
43 || ''''
44 || 'Other'
45 || '''')
46 ELSE
47 NULL
48 END
49 FROM DUAL)
50
SQL>
SQL>
SQL> /
COUNT(*)
126
0Second Query
SQL> var name varchar2(10)
SQL> exec :name:='BS&I'
PL/SQL procedure successfully completed.
SQL> set define off
SQL> SELECT COUNT (*) --sr_bus_unit,count(trunc(sr_open_date))
2 FROM vwsr_all_merged_data s
3 WHERE sr_tio_priority IN
4 ('Level 0', 'CEO', 'TER', 'Priority Assistance', 'Enquiry')
5 AND sr_status = 'Open'
6 AND SR_BUS_UNIT IN
7 (SELECT CASE
8 WHEN :NAME = 'BS&I'
9 THEN
10 ('Business Support and Improvement')
11 ELSE
12 NULL
13 END
14 FROM DUAL
15 UNION
16 SELECT CASE
17 WHEN :NAME = 'BS&I'
18 THEN
19 ('Finance and Administration')
20 ELSE
21 NULL
22 END
23 FROM DUAL
24 UNION
25 SELECT CASE
26 WHEN :NAME = 'BS&I'
27 THEN
28 ('Cust Sat Simplification and Productivity')
29 ELSE
30 NULL
31 END
32 FROM DUAL
33 UNION
34 SELECT CASE
35 WHEN :NAME = 'BS&I'
36 THEN
37 ('Project New and Customer Experience')
38 ELSE
39 NULL
40 END
41 FROM DUAL
42 UNION
43 SELECT CASE
44 WHEN :NAME = 'BS&I'
45 THEN
46 ('Corp Strategy and Customer Experience')
47 ELSE
48 NULL
49 END
50 FROM DUAL
51 UNION
52 SELECT CASE
53 WHEN :NAME = 'BS&I'
54 THEN
55 ('Other')
56 ELSE
57 NULL
58 END
59 FROM DUAL
60 )
61 /
COUNT(*)--SR_BUS_UNIT,COUNT(TRUNC(SR_OPEN_DATE))
126
SQL>
Maybe you are looking for
-
Reg : Selection-Screen in LDB's
hi, Iam working on a report which uses Logical DataBase : PSJ. Iam getting output based on the standard selection screen of LDB PSJ. Now my requirement is : I need to add a new field(select option) to the Selection screen and the output should be di
-
Is the iPad mini (version 2012) a decent ereader
I've read some reviews stating that the iPad mini doesn't do well reading outside in sunlight. I'm moving up from a Kindle Keyboard which gave great results in sunlight to an iPad mini (2012 non-retinal). Any comments from iPad mini users? Also, the
-
Hi all, Any one who know about oracle tool for Project Management like Microsoft Project, to maintain projects. Also tell from where it could be download and its documentation. Thanks in advance to all.
-
Elements 9 Cloning and Healing problems
My Cloning and Healing Tool doesn't work correctly. Both very often puts what I am cloning or healing (with content aware) somewhere else on the photo other than where I want it. Sometimes it put a large streak accross the photo as I make a small cor
-
Candidate Profile - External Candidates- Data not saving
HI All, I have a developed a custom Web Dynpro application and added it to the Candidate Profile. This Custom Webdynpro application is updating a custom infotype. When I run the candidate profile application for external candidates, it does not save