Using Decode or Case
Hi All,
I have an issue cannot resolve myself, need help.
DA1> desc test_case
Name Null? Type
ID NUMBER
ID_DESC VARCHAR2(30)
CUR_AMT NUMBER
PRV_AMT NUMBER
YEAR VARCHAR2(4)
LOAN_AMT NUMBER
DA1> select * from test_case;
ID ID_DESC CUR_AMT PRV_AMT YEAR LOAN_AMT
10 DAILY 2000 2050 2008 4050
20 Weekly 11000 12000 2008 23000
30 Bi-Weekly 23000 24000 2008 47000
40 Monthly 47000 45000 2008 92000
50 Yearly 500000 490000 2008 990000
We have now decided that for ID 40 and 50 the following is the criteria.
1. the ID_DESC will be concatenated ' (N/A)'
2. the LOAN_AMT will be shown same as that of Bi-Weekly.
Example:
ID ID_DESC CUR_AMT PRV_AMT YEAR LOAN_AMT
40 Monthly (N/A) 47000 45000 2008 47000
50 Yearly (N/A) 500000 490000 2008 47000
I was able to do the follwoing:
DA1> select id,
2 decode(id, 40, id_desc || ' (N/A)',
3 50, id_desc || ' (N/A)',
4 id_desc) id_desc,
5 cur_amt,
6 prv_amt,
7 year,
8 loan_amt
9 from test_case;
ID ID_DESC CUR_AMT PRV_AMT YEAR LOAN_AMT
10 DAILY 2000 2050 2008 4050
20 Weekly 11000 12000 2008 23000
30 Bi-Weekly 23000 24000 2008 47000
40 Monthly (N/A) 47000 45000 2008 92000
50 Yearly (N/A) 500000 490000 2008 990000
I am unable to figure out how to assign the loan_amt for ID 30 to ID 40 and 50.
this query is used in a report.
Note: the actual table and its values are different.
thanks for all the help;
Habeeb
Thanks to all,
Finally I figured it...
Below is the query...
DA1> select id,
2 decode(id, 40, id_desc || ' (N/A)',
3 50, id_desc || ' (N/A)',
4 id_desc) id_desc,
5 cur_amt,
6 prv_amt,
7 year,
8 decode(id, 40, (select loan_amt
9 from test_case
10 where id = 30),
11 50, (select loan_amt
12 from test_case
13 where id = 30),
14 loan_amt) loan_amt
15 from test_case;
ID ID_DESC CUR_AMT PRV_AMT YEAR LOAN_AMT
10 DAILY 2000 2050 2008 4050
20 Weekly 11000 12000 2008 23000
30 Bi-Weekly 23000 24000 2008 47000
40 Monthly (N/A) 47000 45000 2008 47000
50 Yearly (N/A) 500000 490000 2008 47000
habeeb
Similar Messages
-
Query to find out sum by using decode or case
Hi, I have data like below and required an output as given below, could you please help me to get the same by using decode / case .....Thanks for your help
INSNAME INSID STATUS
AAA 1000 Scheduled
AAA 1000 Processing
BBB 1001 Inspector
BBB 1001 Scheduled
BBB 1001 Processing
BBB 1001 Inspector
CCC 1002 Scheduled
CCC 1002 Processing
CCC 1002 Inspector
CCC 1002 Scheduled
CCC 1002 Processing
CCC 1002 Inspector
Required Output...
INSNAME INSID sum_of_scheduled sum_of_Processing sum_of_Inspector
AAA 1000 1 1 0
BBB 1001 1 1 2
CCC 1002 2 2 2And if you want it with CASE statement:
WITH test_table AS
SELECT 'AAA' insname, 1000 insid, 'Scheduled' status FROM DUAL UNION ALL
SELECT 'AAA' insname, 1000 insid, 'Processing' status FROM DUAL UNION ALL
SELECT 'BBB' insname, 1001 insid, 'Inspector' status FROM DUAL UNION ALL
SELECT 'BBB' insname, 1001 insid, 'Scheduled' status FROM DUAL UNION ALL
SELECT 'BBB' insname, 1001 insid, 'Processing' status FROM DUAL UNION ALL
SELECT 'BBB' insname, 1001 insid, 'Inspector' status FROM DUAL UNION ALL
SELECT 'CCC' insname, 1002 insid, 'Scheduled' status FROM DUAL UNION ALL
SELECT 'CCC' insname, 1002 insid, 'Processing' status FROM DUAL UNION ALL
SELECT 'CCC' insname, 1002 insid, 'Inspector' status FROM DUAL UNION ALL
SELECT 'CCC' insname, 1002 insid, 'Scheduled' status FROM DUAL UNION ALL
SELECT 'CCC' insname, 1002 insid, 'Processing' status FROM DUAL UNION ALL
SELECT 'CCC' insname, 1002 insid, 'Inspector' status FROM DUAL
SELECT insname
,insid
,SUM(CASE WHEN status = 'Scheduled'
THEN 1
ELSE 0
END
) sum_of_scheduled
,SUM(CASE WHEN status = 'Processing'
THEN 1
ELSE 0
END
) sum_of_processing
,SUM(CASE WHEN status = 'Inspector'
THEN 1
ELSE 0
END
) sum_of_inspector
FROM test_table
GROUP BY insname
,insid;
Regards
Arun -
How to use decode or case function in this?
Hi,
I want to implement this condition in a query without using any user defined functions
1.if (T_fees = 'ABC' then if (p_fees>0) then nvl(p_fees,0) else (p_fees + a_fees) else 0)
2. if(g_fees <> (if t_fees = 'ABC' then if (p_fees>0) then nvl(p_fees,0) else (p_fees)
else 0
Is it possible to use any nested 'CASE' statement ?
This is very urgent...
Regards,
ShilpaHi,
Is it possible to use any nested 'CASE' statement ?Yes it it possible.
"afiedt.buf" 7 lines, 89 characters
1 SELECT sysdate FROM DUAL
2 WHERE 1 = (CASE WHEN 1=1 AND 2=2 THEN
3 1
4 ELSE
5 2
6* END)
SQL>/
SYSDATE
07-AUG-06
1 row selected.
"afiedt.buf" 11 lines, 139 characters
1 SELECT sysdate FROM DUAL
2 WHERE 1 = (CASE WHEN 1=1 AND 2=2 THEN
3 CASE WHEN 3=3 AND 4=4 THEN
4 1
5 ELSE
6 0
7 END
8 ELSE
9 0
10* END)
SQL>/
SYSDATE
07-AUG-06
1 row selected.
You can implement your logic.
Regards -
More effecient way of using Decode and CASE
Is it possible to use both case and decode together? I feel I have too many decode and I am not sure how to write this in a more efficient way.
I would really appreciate any help.
Thank you so much in advance!!!
SELECT Person_ID,Work_ID,
CASE WHEN NBR = 1 THEN
MIN(DECODE(NBR, 1, field1)) aug_one_field1;
MIN(DECODE(NBR, 1, field2)) aug_one_field2;
MIN(DECODE(NBR, 1, field3)) aug_one_field3;
MIN(DECODE(NBR, 1, field4)) aug_one_field4;
MIN(DECODE(NBR, 1, field5)) aug_one_field5;
MIN(DECODE(NBR, 1, field6)) aug_one_field6;
CASE WHEN NBR = 2 THEN
MIN(DECODE(NBR, 2, field1)) aug_two_field1;
MIN(DECODE(NBR, 2, field2)) aug_two_field2;
MIN(DECODE(NBR, 2, field3)) aug_two_field3;
MIN(DECODE(NBR, 2, field4)) aug_two_field4;
MIN(DECODE(NBR, 2, field5)) aug_two_field5;
MIN(DECODE(NBR, 2, field6)) aug_two_field6;
CASE WHEN NBR = 3 THEN
MIN(DECODE(NBR, 3, field1)) aug_three_field1;
MIN(DECODE(NBR, 3, field2)) aug_three_field2;
MIN(DECODE(NBR, 3, field3)) aug_three_field3;
MIN(DECODE(NBR, 3, field4)) aug_three_field4;
MIN(DECODE(NBR, 3, field5)) aug_three_field5;
MIN(DECODE(NBR, 3, field6)) aug_three_field6;
CASE WHEN NBR = 4 THEN
MIN(DECODE(NBR, 4, field1)) aug_four_field1;
MIN(DECODE(NBR, 4, field2)) aug_four_field2;
MIN(DECODE(NBR, 4, field3)) aug_four_field3;
MIN(DECODE(NBR, 4, field4)) aug_four_field4;
MIN(DECODE(NBR, 4, field5)) aug_four_field5;
MIN(DECODE(NBR, 4, field6)) aug_four_field6;
END
FROM (SELECT Person_ID,Work_ID, NBR
fiel1, field2, field3,field4,field5, field6,
FROM field_rep
GROUP BY Person_ID,Work_IDThanks alot John and Frank.
John to answer your question, I just felt the 24 or more decode will slow down the systems performance, hence I was trying to find a better way to do it.
Frank
This is a sample data but I want it pivoted hence the reason for using the decode. I have oracle 10g. These are sample data
Person_id work_id NBR field1 field2 field3 field4 field5 field6
1 ao334 1 1/2/2009 1/9/2010 block_A HH 55667 1
1 ao334 2 5/2/2011 9/9/2013 block_Z HL 11111 3
1 ao334 2 1/2/2009 1/9/2010 block_A HH 22222 1
1 ao334 4 1/2/2009 1/9/2010 block_A HH zzzzz 7
1 z5521 1 10/5/2006 12/31/2012 block_C SS 33322 1
1 z5521 2 1/2/2009 1/9/2010 block_C SS 21550 1
1 z5521 3 1/2/2009 1/9/2010 block_R SS 10000 1
1 z5521 4 1/2/2009 1/9/2010 block_D SS 99100 5
1 z5521 5 1/2/2009 1/9/2010 block_P SS 88860 1
1 z5521 6 1/2/2009 1/9/2010 block_G SS 99660 8
1 ob114 1 1/2/2009 1/9/2010 block_A HH 52333 1
1 ob114 2 1/2/2009 1/9/2010 block_A HH 88888 1Output will look like this
Person_id work_id aug_one_field1 aug_one_field2 aug_one_field3 aug_one_field4 aug_one_field5 aug_one_field6 aug_two_field1 aug_two_field2 aug_two_field3 aug_two_field4 aug_two_field5 aug_two_field6
1 ao334 1/2/2009 1/9/2010 block_A HH 55667 1 5/2/2011 9/9/2013 block_Z HL 11111 3 -
I am having issue in group by using decode and case in pl/sql
my query is some thing like this but having more column in select. when i am firing this query it is giving result but that is not proper
my problem is , like if there are 3 more values for uh.sflowtype (0,1,2) then group by is not working for them and those are coming in different row , i need them to be combined
query is :
select substr(uh.sstartdatetime,1,8) DateTime,
( case
when uh.sflowtype=7 then 'sms'
when uh.sflowtype=9 then 'mms'
when uh.sflowtype=10 then 'gprs'
else 'voice'
end )
as flowtype from e_vpn_usagehistory uh where 1=1 and uh.nspid='1' AND ((substr(uh.sstartdatetime,1,8) >= 20130507 )
AND (substr(uh.sstartdatetime,1,8) <= 20130606)) GROUP BY substr(uh.sstartdatetime,1,8),uh.sflowtype
order by substr(uh.sstartdatetime,1,8) DESC
result :
DATETIME FLOWTYPE
20130507 voice
20130507 voice
20130507 voice
20130507 sms
20130507 mms
but i need
20130507 voice
20130507 sms
20130507 mms
so what should i do?
please suggest me996962 wrote:
my query is some thing like this but having more column in select. when i am firing this query it is giving result but that is not proper
my problem is , like if there are 3 more values for uh.sflowtype (0,1,2) then group by is not working for them and those are coming in different row , i need them to be combined
query is :
select substr(uh.sstartdatetime,1,8) DateTime,
( case
when uh.sflowtype=7 then 'sms'
when uh.sflowtype=9 then 'mms'
when uh.sflowtype=10 then 'gprs'
else 'voice'
end )
as flowtype from e_vpn_usagehistory uh where 1=1 and uh.nspid='1' AND ((substr(uh.sstartdatetime,1,8) >= 20130507 )
AND (substr(uh.sstartdatetime,1,8) <= 20130606)) GROUP BY substr(uh.sstartdatetime,1,8),uh.sflowtype
order by substr(uh.sstartdatetime,1,8) DESC
result :
DATETIME FLOWTYPE
20130507 voice
20130507 voice
20130507 voice
20130507 sms
20130507 mms
but i need
20130507 voice
20130507 sms
20130507 mms
so what should i do?
please suggest meWell, you are grouping by substr(uh.sstartdatetime,1,8),uh.sflowtype
so different values of sflowtype that aren't 7,9 or 10 will be grouped separately.
You therefore need to group by your case statement instead of just the sflowtype.
The most worrying thing though is that you have sstartdatetime that appears to be a varchar2 and not a date.
If it is a date, you are applying a substr (which applies to strings) to a date. you then compare the result
of the substr to a number.
This is a recipe for future problems: sort it out using the correct conversion functions with format masks.
Edited by: Paul Horth on May 14, 2013 1:25 PM -
Using Decode Or Case Statement / Any Method
Dear All,
In my table Fields are
EmpId
TranYear
TranMonth
ApprovedBy
ApprovedYear
ApprovedMonth
My Input parameters are
1. TranYear
2. TranMonth
3. Tag1
4. Tag2
My requirement is
Tag1 Input are 0,1,2
Tag2 Inputs are 0,1,2
If Tag1 is 0 then checking TranYear/TranMonth And
Tag2 Is 0 Then ApprovedBy Is Not Null only
Tag2 Is 1 Then ApprovedBy Is Null only
Tag2 Is 2 Then both(Approved/unApproved)
If Tag1 is 1 then checking ApprovedYear/ApprovedMonth And Tag2 Passing 0 Default
If Tag1 is 2 then checking TranYear/TranMonth Or ApprovedYear/ApprovedMonth And
Tag2 Is 0 Then ApprovedBy Is Not Null only
Tag2 Is 1 Then ApprovedBy Is Null only
Tag2 Is 2 Then both(Approved/unApproved)
Based upon the input details select detailsdont quite understand your table structure requirement but it sounds like you want a conditional join.
looks like you are always joining on the tran year tran month but according to your 2 flags decides the rest of the join
I started something here where I am joining on the tran year / tran month then setting an indicator based on your flags
then just select the rows where this indicator is true.
anyway you can modify it to your needs
WITH t AS (SELECT 1 EmpId,
EXTRACT (YEAR FROM TO_DATE ('2010', 'YYYY')) TranYear,
EXTRACT (MONTH FROM TO_DATE ('Oct', 'Mon')) TranMon,
10 approved_by,
EXTRACT (YEAR FROM TO_DATE ('2010', 'YYYY')) ApprovedYear,
EXTRACT (MONTH FROM TO_DATE ('Nov', 'Mon')) ApprovedMon
FROM DUAL
UNION ALL
SELECT 2,
EXTRACT (YEAR FROM TO_DATE ('2009', 'YYYY')),
EXTRACT (MONTH FROM TO_DATE ('Jul', 'Mon')),
11,
EXTRACT (YEAR FROM TO_DATE ('2009', 'YYYY')),
EXTRACT (MONTH FROM TO_DATE ('Dec', 'Mon'))
FROM DUAL),
input AS (SELECT EXTRACT (YEAR FROM TO_DATE ('2010', 'YYYY')) TranYear,
EXTRACT (MONTH FROM TO_DATE ('Oct', 'Mon')) TranMon,
0 tag1,
0 tag2
FROM DUAL)
SELECT *
FROM (SELECT t.*,
tag1,
tag2,
CASE tag1 || '.' || tag2
WHEN '0.0'
THEN
CASE WHEN Approved_By IS NOT NULL THEN 1 ELSE 0 END -- if they are both 0 join on Tran Year, Tran Month and Approved by not null
WHEN '0.1'
THEN
CASE WHEN Approved_By IS NULL THEN 1 ELSE 0 END -- if they are 0,1 then Tran Year, Tran Month and Approved by is null
WHEN '0.2'
THEN
0 -- CASE --Then both(Approved/unApproved) i Don't know what this means
/* WHEN '1.0' then whatever
when '1.1' then whatever
when '1.2 then whatever
ELSE
0
END
ind
FROM t, input
WHERE t.TranYear = input.TranYear AND t.TranMon = input.TranMon)
WHERE ind = 1 -
I have one table which stores candidates' response. The structure is like this
(Seatno, Questionnumber, Answer).
And I have another table Master as (Questionnumber, Answer)
Now I want to calculate marks by comparing Candidate's response with Master.
I tried to use decode and case. But was not successful.
My query with decode was ...
select c1.candidatesrno, sum(decode(c1.answer,(c1.answer=m1.answer),1,0))
from candidate c1,master q1
where c1.question=m1.question
group by c1.candidatesrno
And query with CASE was...
select c1.candidatesrno,
case when (c1.answer=m1.answer) then 1 else 0
end resultset
from candidate c1,master m1
where c1.questionnumber=m1.questionnumber
group by c1.candidatesrno
Can anybody help ?I want to compare candidate's response with master
table.
Now for each question of Candidate, if its answer
matches with answer in Master table I have to give 1
marks...if it does not match I have to give 0
marks...like this there are two-three conditions...
Something like this ?
test@ORA10G>
test@ORA10G> with master as (
2 select 'Q1' as question_num, 'A1' as answer_num from dual union all
3 select 'Q2', 'A2' from dual union all
4 select 'Q3', 'A3' from dual),
5 candidate_response as (
6 select 'S1' as seat_num, 'Q1' as question_num, 'A1' as answer_num from dual union all
7 select 'S1', 'Q2', 'A2' from dual union all
8 select 'S1', 'Q3', 'A3' from dual union all
9 select 'S2', 'Q1', 'A5' from dual union all
10 select 'S2', 'Q2', 'A6' from dual union all
11 select 'S2', 'Q3', 'A3' from dual)
12 --
13 select
14 cr.seat_num,
15 cr.question_num,
16 cr.answer_num,
17 case when m.question_num is null and m.answer_num is null then '0 point'
18 else '1 point'
19 end as points
20 from candidate_response cr, master m
21 where cr.question_num = m.question_num(+)
22 and cr.answer_num = m.answer_num(+)
23 order by cr.seat_num, cr.question_num, cr.answer_num;
SE QU AN POINTS
S1 Q1 A1 1 point
S1 Q2 A2 1 point
S1 Q3 A3 1 point
S2 Q1 A5 0 point
S2 Q2 A6 0 point
S2 Q3 A3 1 point
6 rows selected.
test@ORA10G>
test@ORA10G> with master as (
2 select 'Q1' as question_num, 'A1' as answer_num from dual union all
3 select 'Q2', 'A2' from dual union all
4 select 'Q3', 'A3' from dual),
5 candidate_response as (
6 select 'S1' as seat_num, 'Q1' as question_num, 'A1' as answer_num from dual union all
7 select 'S1', 'Q2', 'A2' from dual union all
8 select 'S1', 'Q3', 'A3' from dual union all
9 select 'S2', 'Q1', 'A5' from dual union all
10 select 'S2', 'Q2', 'A6' from dual union all
11 select 'S2', 'Q3', 'A3' from dual)
12 --
13 select cr.seat_num, count(*) as score
14 from candidate_response cr, master m
15 where cr.question_num = m.question_num
16 and cr.answer_num = m.answer_num
17 group by cr.seat_num
18 order by cr.seat_num;
SE SCORE
S1 3
S2 1
test@ORA10G>
test@ORA10G>pratz -
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 -
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; -
Using 'DECODE' instead of 'CASE' in select
Hi,
I am using CASE statements in my Select clause using 9i.
SELECT
CASE
WHEN S.cmpy_num = S.cpty_borg_num THEN
''OURS''
ELSE
''THEIRS''
END AS SDIOwner,
S.active_flag,
from SDI sI wud like to try this in 8I but 8i doesnt support CASE statements
in Select clauses.
It was recommended to use Decode in place of CASE in 8I
How can i write the decode statement for the above CASE statement in 9IGOT IT RUNNIG.THANKS A LOT JAMES...
I AM TESTING SYSTEMS ON 9I AND 8I... -
Using function on decode or case in query
Hi experts
I am using oracle 11G database,I have to check length of name column value from employee table and if length(name) > 39 then value should be substr(name,0,39)
else value should be name only.
i tried below code
select CASE when length(name) > 39,substr(name,0,39)
else name
END
from employee but its not working ..can I do this using decode too ? ,,which one would be better or this is not a right way ?
ThanksHi,
siebelD wrote:
Hi experts
I am using oracle 11G database,I have to check length of name column value from employee table and if length(name) > 39 then value should be substr(name,0,39)
else value should be name only.
i tried below code
select CASE when length(name) > 39,substr(name,0,39)
else name
END
from employee but its not working ..Review the syntax of the CASE expression in the SQL Language manual. A CASE expression always has at least one THEN clause. Commas are not part of the CASE expression syntax.
can I do this using decode too ? Sure, anything you can do with CASE you can also do with DECODE.
,,which one would be betterThis is one of the many situations where CASE is much shorter, clearer and more efficient than DECODE.
or this is not a right way ?How about
SELECT SUBSTR (name, 1, 39) AS employee_name
FROM employee;?
If name is 39 characters (or less), then <tt> SUBSTR (name, 1, 39) </tt> will return it, unchanged.
Edited by: Frank Kulash on Mar 3, 2013 2:07 PM -
I need to know if there is any way to use a range of values from
database and decode to certain text. I am able to do with one
value.
for example:
DECODE(column_name,'216767111','Unlimited',column_name)
above argument works with one value only. How about a range,
ex: 216767000 to 216767111. I need to use only SQL. No PL/SQL.
Kinldly need some body's help
Thanks
MunisWhich version of the database? If it's 8i+ then you can use
the CASE function
for example:
(CASE WHEN column_name between 216767000 and 216767111
THEN 'Unlimited' ELSE column_name END)
This won't work in PL/SQL because they're introducing a CASE
statement does soemthing different.
rgds, APCHello Andrew
Thank you for response. I am using 8i. 8.1.6. However using
CASE, I get inconsistent data type, ORA-00932: inconsistent
datatypes. I able to work it out with other response using
DECODE(sign(. Do you have any idea why i am getting this error.
If time permits, let me know -
Using Decode in Expression Builder...
Hi everybody,
I want to use the DECODE function of SQL in an Expression Builder....
Of course this function does not exist in any of the transformation categories(arithmetic,character,e.t.c.) in the expression builder.
I have read in the OWB on-line help that in the Expression Builder there should be an 'Others' drop - down list in order to select any of the available SQL clauses - one of them is 'decode'...
The problem is that this drop - down list in the Expression Builder is disabled.....
How can I use this function anyway..????
Instead of using ... in the Expression Builder...
decode(<column_name>,'1','val_one','2','val_two') what can I use to do the same....???
As regards the problem that the drop-down list is disabled , what may be the cause..???
I use Microsoft XP machine , Oracle 10g (v.10.2) and OWB v.10.1.0.4
The documentation says....
"This allows you to deploy the DECODE functionality in all operating modes (setbased, rowbased, etc.) and transparently across Oracle database releases (8.1, 9.0 and 9.2)."
The above means that the cause is the incompatibility..????
Thanks , a lot
SimonI am using OWB 9.2.0.8 and I can't reproduce the problem you are having with 10.1, but did you try just typing it into the expression?
Or, better yet, use CASE. The syntax is easier to read. I think OWB translates DECODEs to CASE when the SQL is generated.
(Case <column_name>
When '1' Then 'val_one'
When '2' Then 'val_two'
End) -
DECODE OR CASE - Which is better and why
Oracle version: 11.2
Problem: We have a huge table with 10M records, which needs to be processed daily.
While loading data in table we have to handle condition if flag is =1 then '111' else '000'
To implement this which one is efficient solution? .. using CASE or DECODE and why?
Example:
SQL> select flag,case when flag='Y' then '111' else '000' end from (select 'Y' as flag from dual union all select 'N' from dual);
F CAS
Y 111
N 000
SQL> select flag,decode(flag,'Y','111','000') from (select 'Y' as flag from dual union all select 'N' from dual);
F DEC
Y 111
N 000Hi,
For this job, they're equally efficient.
For any job, where DECODE doesn't require a lot more code than CASE, they will be equally efficient.
I recommend (almost) always using CASE. It's easier to understand and debug, and, if written correctly, will never be slower than DECODE.
The only situation where I use DECODE is for very simple tasks (like the one you posted) where this is used inside a very complicated expression, and the slightly less coding needed for DECODE makes the larger statement easier to read. -
How to use DECODE function in Exspression?
Hi,
Can we use DECODE in Expression?
I'm trying to use DECODE function but there is an error during the validation. But when i validate the mapping, it is successfully compiled but it is failed during deployment.
But if I use CASE instead of DECODE, it works fine.
Can we use DECODE in OWB???
Thanks
RajHi,
In OWB 10gR2, if your are using only one DECODE in an expression, it's working. The package will compile when deploying the mapping. OWB will replace the DECODE by a CASE.
But when you are using nested decode in an expression ( for example : decode(col1, 1, 'M', decode(col2, 'Madame', 'Mme', null)) ) only the first one is replaced by a case at deployment.
In ROW_BASED mode, text of the expression is used outside of an sql statement and deployment will fails with "PLS-00204: function or pseudo-column 'DECODE' may be used inside a SQL statement only."
If operating mode for the mapping is set to SET_BASED, it's working because the expression is used in an sql statement.
I have logged a SR in metalink for this issue and a bug is opened (bug 5414112).
But I agree with you, it's better to use case statement.
Bernard
Maybe you are looking for
-
How to call a LV.exe from LV via ActiveX?
Hi all, I need some kind of atomic task, e.g. some (short) instructions that cannot be aborted. Calling a DLL, wait some ms, calling it again... And even an Abort via VI server shall not abort this atomic task but finish it. I thought about building
-
Communication between non SAP and SAP
HI, do you have some information about communication between non SAP and ERP (WebAs) SAP System ? I would like to receive an xml file or xml IDoc from a Java Plattform (non SAP) into our SAP ERP System as an own defined IDoc. remember: we don´t have
-
As I'm unable to add music to my iPod any longer, I tried updating. I'm unable to achieve the updating cause as soon as I launch the update process, a meesage with "Internal error of iPod manager" appears. Help plaese
-
Is there a way to put metadata into an iWeb website?
I'm creating a website using iWeb which has a Facebook share button. I need to add some metadata to the web page's head section so that Facebook will pick up the correct content. Is there a simple way to do this without needing to manually edit the i
-
Lightroom 4.4 update problem
when i try to update LR 4.3 to 4.4, it keeps downloading 4.3 again.