Converting Decode to Case
I'm a beginner with Oracle SQL and I have a select statement with the following code:
max(decode(EDW.V_RECRUIT_TEST_RESULT_HIST.TEST_CD, '1000', EDW.V_RECRUIT_TEST_RESULT_HIST.TEST_RESULT_TEST_SCORE, '2000', EDW.V_RECRUIT_TEST_RESULT_HIST.TEST_RESULT_TEST_SCORE, '4000', EDW.V_RECRUIT_TEST_RESULT_HIST.TEST_RESULT_TEST_SCORE))
I've been asked to create several more columns in a similar manner. My concern is that since this relies on 'decode' which is less efficient than 'case', that adding more columns using this approach will bog down an already not efficient query. Bottom line my problem is that I don't really understand this 'decode' since all the explanations of 'decode' I've found stop at four parameters.
Could someone please show me how the expression above looks in If-then-else terms as well as comment on how to convert this 'decode' to 'case'? Thanks in advance!
1) The if-then-else stmt
-- Longest way
if EDW.V_RECRUIT_TEST_RESULT_HIST.TEST_CD = '1000' then
return value of EDW.V_RECRUIT_TEST_RESULT_HIST.TEST_RESULT_TEST_SCORE;
elsif EDW.V_RECRUIT_TEST_RESULT_HIST.TEST_CD = '2000' then
return value of EDW.V_RECRUIT_TEST_RESULT_HIST.TEST_RESULT_TEST_SCORE;
elsif EDW.V_RECRUIT_TEST_RESULT_HIST.TEST_CD = '4000' then
return value of EDW.V_RECRUIT_TEST_RESULT_HIST.TEST_RESULT_TEST_SCORE;
else return null;
end if;
-- shorter way
if EDW.V_RECRUIT_TEST_RESULT_HIST.TEST_CD = '1000' or
EDW.V_RECRUIT_TEST_RESULT_HIST.TEST_CD = '2000' or
EDW.V_RECRUIT_TEST_RESULT_HIST.TEST_CD = '4000' then
return value of EDW.V_RECRUIT_TEST_RESULT_HIST.TEST_RESULT_TEST_SCORE;
else return null;
end if;
-- shortest if-then-else code
if EDW.V_RECRUIT_TEST_RESULT_HIST.TEST_CD in ('1000', '2000', '4000') then
return value of EDW.V_RECRUIT_TEST_RESULT_HIST.TEST_RESULT_TEST_SCORE;
else return null;
end if;
2) The MAX() function
The MAX function will return the greatest value of EDW.V_RECRUIT_TEST_RESULT_HIST.TEST_RESULT_TEST_SCORE in step 1 above.
2) Using CASE
select
max(case when (EDW.V_RECRUIT_TEST_RESULT_HIST.TEST_CD in('1000','2000','4000'))
then EDW.V_RECRUIT_TEST_RESULT_HIST.TEST_RESULT_TEST_SCORE end)
into ...your_item_or_local_variable
from EDW.V_RECRUIT_TEST_RESULT_HIST;
Try the code above, good luck
v/r
Vien Tran
Similar Messages
-
Please convert decode query to case expression
Please convert decode query to case expression
Decode(Sign(id), 1, id, null) positive, Decode(Sign(id), -1, id, null) negative from dual;this is a serious forums that help people, if you want that we do your "homeworks" i must told you that you should pay us for that.
Edited by: Fran on 05-jun-2013 23:41
1002966
Handle: 1002966
Status Level: Newbie
Registered: Apr 28, 2013
Total Posts: 20
Total Questions: 12 (12 unresolved) -
Converting Decode function in OBIEE
Hi Guru's
we are converting some BO reports to OBIEE and need to convert Decode function
Below is the Decode function they are using:
Decode ( sign(nvl(BOOKINGS_DATA_UDTC.qty_shipped,0)), 0,decode(BOOKINGS_DATA_UDTC.DELIVERY_NUMBER, 0,decode(BOOKINGS_DATA_UDTC.Schd_Ship_date_fk,to_date('1/1/1990','mm/dd/yyyy'),'Next Month Backlog',decode(sign(nvl(BOOKINGS_DATA_UDTC.Schd_Ship_date_fk, '01-JAN-90') -PAR.PAR_DATE), 1, 'Next Month Backlog', 'Current month will ship')) ,'Awaiting for collection') ,'MTD Shipped')
i have tried converting into case :
case when sign(IFNULL("BOOKINGS_DATA_UDTC"."Qty Shipped",0)) = 0 then (case when "BOOKINGS_DATA_UDTC"."Delivery Number" = 0 then (case when "BOOKINGS_DATA_UDTC"."SCHD_SHIP_DATE_FK" = '01-JAN-1990' then 'Next Month Backlog' else (case when Timestampdiff(SQL_TSI_DAY, cast('@{ParDate}{28-DEC-2012}' as Date),(case when "BOOKINGS_DATA_UDTC"."SCHD_SHIP_DATE_FK" IS NULL then CAst('01-JAN-1990' as DATE) else "BOOKINGS_DATA_UDTC"."SCHD_SHIP_DATE_FK" end)) > 1 then 'Next Month Backlog' else 'Current Month Will Ship' end) end) else 'Awaiting For Collection' end) else 'MTD Shipped' end
But it is not workign as expected.
Can some one please help me with this.
Thanks,you can achieve it by using CASE WHEN condition END function in obiee. nested case also supported by obiee.
check the below link Decode Join Condition in OBIEE RPD
Thanks
Jay.
Edited by: Jay on Apr 3, 2012 12:56 PM -
Hi all,
select
id
,one
,two
From
(select id
, one
, two
, Max(decode(TYPE, "ER", 3*4, 4*7)
from
(select id
, sum(one) as one
, sum(two) as two
from t1
group by id)
group by id, one, two
) temp inner join t2....
The above query is just a sample structure to tell the problem that i am facing. It works fine, but i need to convert it to ANSI Standard.
Hence I need to convert the DECODE to CASE statement. but if i convert to CASE, i need to use any of the GROUP BY FUNCTIONS, but if i use the MAX of any group by functions, the result would be wrong...how to overcome it
ThanksHi,
Yes I did a silly mistake, the code I gave was for only the decode but when I converted it to case statement, there was a silly mistake, which i corrected.
Now i corrected it to
Select
COL1
, Col2
, MAX(CASE WHEN Upper(TYPE) = 'YR' THEN Round(3.43*3, 2) END) AS CALC1
, Max(CASE WHEN Upper(TYPE) = 'ZP' THEN Round(3.12*12, 2) END) AS CALC2
From
Group by....
but now when i add a sum of the calc1 & calc2, I am not getting any results the column is blank. Is there anything that i missed
Select
COL1
, Col2
, MAX(CASE WHEN Upper(TYPE) = 'YR' THEN Round(3.43*3, 2) END) AS CALC1
, Max(CASE WHEN Upper(TYPE) = 'ZP' THEN Round(3.12*12, 2) END) AS CALC2
, (MAX(CASE WHEN Upper(TYPE) = 'YR' THEN Round(3.43*3, 2) END
+ Max(CASE WHEN Upper(TYPE) = 'ZP' THEN Round(3.12*12, 2) END)) as SUM3
From
Group by....
Thanks -
ISE 1.2 - Guest Account converted to lower-case automatically
Hello
I have an ISE appliance version 1.2 and sponsor portal
I create accounts with upper case username and upper case password, but Sponsor portal convert it to lower case.
I try to login with lower case or upper case. I can't login with both.Check the Multiport configurations and HTML page settings for converting the Alphabetic-Cases.:
You can check the below link for step by step configuration of HTML-Page’s setting:
Link-1
http://www.cisco.com/en/US/docs/security/ise/1.2/user_guide/ise_custom_portals.html
Link-2
http://www.cisco.com/en/US/docs/security/ise/1.0/sponsor_guide/ise10_sponsor.html#wp1069407 -
Problem in converting into upper case urgent
hi,
i working on module pool program.
in the initial screen there is two fields one is number one is name.
if i enter the name in the name field it is automatically converting into upper case,
but in this case my select query is not working,how to solve this,i mean i have to take same as it is what i entered.
kindly give me suggestion.it is urgent issue.i have to delever today.
Thanks,
mohan.hi
in the Report to handle like this situation.. we use the extentions to the parameter as LOWER CASE .
parameters p_t type char10 LOWER CASE .
i think in the Module pool also.. we can do this in Properties of the FIELD... once check the Properties of the field... there could be a chance.
hi
<b>there is field <b>checkbox called UPPER/LOWER CASE</b>at the bottom of the properties... if tick this u r porblem will be solved</b>
Please Close this thread.. when u r problem is solved
Reward if Helpful
Regards
Naresh Reddy K
Message was edited by:
Naresh Reddy -
Hi
I need small clarification about difference between decode and case
Thanks who visit my threadAnd for those people who can't be ar$ed to follow links...
Decode allows for conditional output where the conditions are straightforward matches between values.
Case allows for regular conditions to be used such as greater than, less than etc.
Some of the things done by case can be achieved through decode as well but in a more convoluted manner than simply using case.
;) -
What will be good for proformance wise:
Decode or Case in a sql statement.?????See the following link for Tom Kyte's opinion (point #4 in his first answer):
http://asktom.oracle.com/pls/ask/f?p=4950:8:16717708356827415201::NO::F4950_P8_DISPLAYID,F4950_P8_CRITERIA:1243867216406 -
Which is the best decode or case
Hi,
When you check performance wise which is the best one decode or case?
Thanks,> You mean CPU processor speed or oracle buffer(SGA).
Neither. CPU architecture. RISC vs CISC vs ..
On a PA-RISC1 CPU a DECODE is just a tad faster than a CASE. On an AMD64 CPU, the reverse is true.
> When I increase memory, The case and decode performance will increase?
No. A CASE and a DECODE does not need memory to work faster. It is a set of machine code instructions that needs to compare values to determine a result. It depends on just how fast the CPU can execute this set of machine code instructions.
A faster CPU will make a very significant difference. An AMD64 Opteron CPU is a couple of times faster than a PA-RISC1 CPU.
I had this exact same conversation back in 2006 on this forum - and posted [url
http://forums.oracle.com/forums/thread.jspa?messageID=1346165�]this benchmark to show that the decision of using CASE or DECODE is not a decision that should be based on raw performance. -
I have emp_allocation table. It has data as below
EMPID YEAR MONTH
X 2006 JAN
X 2006 MAR
Y 2006 JAN
Y 2006 FEB
Y 2006 MAR
I want SQL Query(Without Decode or Case) which will give output as below
EMPID YEAR JAN FEB MAR APR JUN JUL
X 2006 Y N Y
Y 2006 Y Y YWhy you'd want to do it this way I do not know, but if you insist...
SQL> ed
Wrote file afiedt.buf
1 WITH t AS (select 'X' AS EMPID, 2006 AS YEAR, 'JAN' AS MONTH FROM DUAL UNION ALL
2 select 'X', 2006, 'MAR' FROM DUAL UNION ALL
3 select 'Y', 2006, 'JAN' FROM DUAL UNION ALL
4 select 'Y', 2006, 'FEB' FROM DUAL UNION ALL
5 select 'Y', 2006, 'MAR' FROM DUAL)
6 -- END OF TEST DATA
7 SELECT EMPID, YEAR, NVL(MAX(JAN),'N') AS JAN, NVL(MAX(FEB),'N') AS FEB, NVL(MAX(MAR),'N') AS MAR, NVL(MAX(APR),'N') AS APR
8 FROM
9 (
10 SELECT EMPID, YEAR, 'Y' AS JAN, NULL AS FEB, NULL AS MAR, NULL AS APR FROM t WHERE MONTH = 'JAN' UNION ALL
11 SELECT EMPID, YEAR, NULL AS JAN, 'Y' AS FEB, NULL AS MAR, NULL AS APR FROM t WHERE MONTH = 'FEB' UNION ALL
12 SELECT EMPID, YEAR, NULL AS JAN, NULL AS FEB, 'Y' AS MAR, NULL AS APR FROM t WHERE MONTH = 'MAR' UNION ALL
13 SELECT EMPID, YEAR, NULL AS JAN, NULL AS FEB, NULL AS MAR, 'Y' AS APR FROM t WHERE MONTH = 'APR'
14 )
15 GROUP BY EMPID, YEAR
16* ORDER BY 1,2
SQL> /
E YEAR J F M A
X 2006 Y N Y N
Y 2006 Y Y Y N
SQL> -
Hi,
What is the difference between decode and case?
What are the cases in which we can use Decode and case
Thanxyou can not put Search CASE statements by using DECODE
Eg:
SELECT AVG(CASE WHEN e.salary > 2000 THEN e.salary ELSE 2000 END) "Average Salary" FROM employees e;Can't we?
select avg(decode(sign(e.salary - 2000), 1, e.salary, 2000)) "Average Salary" from employees e; -
Value converting to upper case
Hi,
There is a table control, but when i am taking a value from the table in PAI of my screen, the value gets converted to upper case, can any one suggest me what would be happening. Is there any way to control this?
Thanks in Advance,
kushagra sharmaHi Sharma,
Go to the screen painter for the table control screen, go to the screen attributes of the field (field you want to restrict the case change) and check the check box Upper/Lower case.
Hope this may help you.
Regards,
Smart Varghese -
[IDCS2-PC] PMString - convert to upper case
Hi everybody,
does somebody meet the problem that some of character couldn't be converted to upper case.
I have
PMString initialized with text
"lørdag". When I call function
ToUpper() I got
"LøRDAG", where there is one character in lower case which wasn't transformed.
Do somebody know why and which function should I used instead of that to be succesfull?
ThanX,
pysoHi everybody,
I've just found a solution. In SDK there is probably error because
PMString.ToUpper() doesn't work well but
WideString.ToUpper() works fine.
bye,
pyso -
What is better and fast to use between decode and case
Hi friends,
i wanted to know what is better to use decode or case in sql,which gives faster result.
thks
sonal....Here's a very simple timing comparison. This table (actually it's a partition) has a little over 1 million rows.
As you can see, the timing difference is trivial. (I ran an earlier query to fetch the blocks from disk).
SQL> select sum(decode(balloon_flag
2 ,'Y',1
3 ,0
4 )
5 ) bal_count
6 from prod.loan_master
7 where report_date = to_date('31-DEC-2005');
BAL_COUNT
9036
Elapsed: 00:00:07.65
SQL> select sum(case balloon_flag
2 when 'Y' then 1
3 else 0
4 end
5 ) bal_count
6 from prod.loan_master
7 where report_date = to_date('31-DEC-2005');
BAL_COUNT
9036
Elapsed: 00:00:07.68
SQL> select sum(case
2 when balloon_flag = 'Y' then 1
3 else 0
4 end
5 ) bal_count
6 from prod.loan_master
7 where report_date = to_date('31-DEC-2005');
BAL_COUNT
9036
Elapsed: 00:00:07.46 -
How does decode and case works?
Hi,
I want to know how decode and case works? How they process the arguements. Please explain me with some examples.
Thanks,
Sarav
Edited by: 943941 on Jul 3, 2012 1:42 AMwelcome
check this link
https://forums.oracle.com/forums/ann.jspa?annID=1535
you will find everything you need
DECODE compares expr to each search value one by one. If expr is equal to a search, then Oracle Database returns the corresponding result. If no match is found, then Oracle returns default. If default is omitted, then Oracle returns null.
This example decodes the value warehouse_id. If warehouse_id is 1, then the function returns 'Southlake'; if warehouse_id is 2, then it returns 'San Francisco'; and so forth. If warehouse_id is not 1, 2, 3, or 4, then the function returns 'Non domestic'.
SELECT product_id,
DECODE (warehouse_id, 1, 'Southlake',
2, 'San Francisco',
3, 'New Jersey',
4, 'Seattle',
'Non domestic')
"Location of inventory" FROM inventories
WHERE product_id < 1775;Edited by: user 007 on Jul 3, 2012 2:40 PM
Maybe you are looking for
-
How to use BAPI Program for change workcenter in co02?
Hi Abapers. Anybody please tell me how to change the workcenter in CO02 using BAPI Programe. Please give me sample of this. I will give urs reward of points. Thanks Regards, S.Muthu.
-
Photoshop Elements 12 keeps freezing while I'm editing my pics.
I have photoshop elements 12 and it keeps freezing on me while I'm in the middle of editing my photos. I have window's 8 on my pc. Is there a reason why this could be happening? (problem with window's 8, installed incorrectly?)
-
My browser wants me to buy a new Mac
Okay so I'm running 10.5.8 on a 2009 MBP. I also run Firefox and it did its automatic update and I've been informed that my OS is no longer supported. It's recommendation? To buy a new Mac. Now spending £1000 on a new laptop is currently not an optio
-
Analytical Function - COUNT issue
Hello All, DB Oracle 11g running on Linux x86 64-bit platform .. I'm curious to know why the below two sets of queries produce different reults. Q 1 WITH TEMP AS SELECT 'ONE' VAL, 5 PAR FROM DUAL UNION ALL SELECT 'ONE' VAL, 7 PAR FROM DUAL UNION ALL
-
Add subscript in Dreamweaver code screen
In Dreamweaver - How do you add HTML elements to the favorites insert menu that are not on the list? For example, I want to add <sub> and <sup> so I can highlight and code math equations> Thanks