CASE WHEN and DECODE
Hi everybody,
I have column in Discoverer like this one shown bellow:
DECODE(Vrstapolise,1,'Aktivno',4,'Kapitalisano',3,DECODE(Stanjepreisteka,'Aktivno','Aktivno','Kapitalisano','Kapitalisano',NULL),NULL)
I want to make the same column with same functionality in OBI BMM. Is it possible? If it is possible, how to do that?
Thanks in advance.
As Sai said use Case stmt, your code will something like this
Case Vrstapolise
When 1 then 'Aktivno'
When 4 then 'Kapitalisano'
When 3 then
Case Stanjepreisteka
when 'Aktivno' then 'Aktivno'
when 'Kapitalisano' then 'Kapitalisano'
else Null end
else Null End
Thanks,
Vino
Similar Messages
-
Case statement and Decode function both are not working in Select cursor.
I have tried both the Case statement and Decode function in Select cursor, but both the things are not working. On the other hand both the things work in just select statement.
See the first column in select (PAR_FLAG), I need to have this evaluated along with other fields. Can you please suggest some thing to make this work. And also I would like to
know the reason why decode is not working, I heard some where Case statement do not work with 8i.
Author : Amit Juneja
Date : 06/20/2011
Description:
Updates the Diamond MEMBER_MASTER table with the values from
INC.MEM_NJ_HN_MEMBER_XREF table.
declare
rec_cnt number(12) := 0;
commit_cnt number(4) := 0;
cursor select_cur is
Select DECODE(1,
(Select 1
from hsd_prov_contract R
where R.seq_prov_id = PM.seq_prov_id
and R.line_of_business = H.line_of_business
and R.PCP_FLAG = 'Y'
and R.participation_flag = 'P'
and SYSDATE between R.EFFECTIVE_DATE AND
NVL(R.TERM_DATE,
TO_DATE('31-DEC-9999', 'DD-MON-YYYY'))),
'Y',
'N') PAR_FLAG,
H.SEQ_ELIG_HIST,
H.SEQ_MEMB_ID,
H.SEQ_SUBS_ID,
H.SUBSCRIBER_ID,
H.PERSON_NUMBER,
H.EFFECTIVE_DATE,
H.TERM_DATE,
H.TERM_REASON,
H.RELATIONSHIP_CODE,
H.SEQ_GROUP_ID,
H.PLAN_CODE,
H.LINE_OF_BUSINESS,
H.RIDER_CODE_1,
H.RIDER_CODE_2,
H.RIDER_CODE_3,
H.RIDER_CODE_4,
H.RIDER_CODE_5,
H.RIDER_CODE_6,
H.RIDER_CODE_7,
H.RIDER_CODE_8,
H.MEDICARE_STATUS_FLG,
H.OTHER_STATUS_FLAG,
H.HIRE_DATE,
H.ELIG_STATUS,
H.PREM_OVERRIDE_STEP,
H.PREM_OVERRIDE_AMT,
H.PREM_OVERRIDE_CODE,
H.SEQ_PROV_ID,
H.IPA_ID,
H.PANEL_ID,
H.SEQ_PROV_2_ID,
H.SECURITY_CODE,
H.INSERT_DATETIME,
H.INSERT_USER,
H.INSERT_PROCESS,
H.UPDATE_DATETIME,
H.UPDATE_USER,
H.UPDATE_PROCESS,
H.USER_DEFINED_1,
H.SALARY,
H.PEC_END_DATE,
H.REASON_CODE,
H.PEC_WAIVED,
H.BILL_EFFECTIVE_FROM_DATE,
H.BILLED_THRU_DATE,
H.PAID_THRU_DATE,
H.SUBSC_DEPT,
H.SUBSC_LOCATION,
H.USE_EFT_FLG,
H.BENEFIT_START_DATE,
H.SEQ_ENROLLMENT_RULE,
H.MCARE_RISK_ACCRETION_DATE,
H.MCARE_RISK_DELETION_DATE,
H.MCARE_RISK_REFUSED_DATE,
H.COMMENTS,
H.USER_DEFINED_2,
H.USER_DEFINED_3,
H.RATE_TYPE,
H.PCPAA_OCCURRED,
H.PRIVACY_ON,
H.PCP_CHANGE_REASON,
H.SITE_CODE,
H.SEQ_SITE_ADDRESS_ID,
PM.seq_prov_id rendered_prov
from hsd_member_elig_history H,
INC.PCP_REASSIGN_RPRT_DATA P,
hsd_prov_master PM
where P.subscriber_id = H.subscriber_id
and P.rendered_pcp = PM.provider_ID
and H.elig_status = 'Y'
and (H.term_date is NULL or H.term_date >= last_day(sysdate))
order by H.Seq_memb_id;
begin
for C in select_cur loop
rec_cnt := rec_cnt + 1;
update hsd_member_elig_history
set term_date = TRUNC(SYSDATE - 1),
term_reason = 'PCPTR',
update_datetime = SYSDATE,
update_user = USER,
update_process = 'TD33615'
where seq_elig_hist = C.seq_elig_hist
and seq_memb_id = C.seq_memb_id;
INSERT INTO HSD_MEMBER_ELIG_HISTORY
(SEQ_ELIG_HIST,
SEQ_MEMB_ID,
SEQ_SUBS_ID,
SUBSCRIBER_ID,
PERSON_NUMBER,
EFFECTIVE_DATE,
TERM_DATE,
TERM_REASON,
RELATIONSHIP_CODE,
SEQ_GROUP_ID,
PLAN_CODE,
LINE_OF_BUSINESS,
RIDER_CODE_1,
RIDER_CODE_2,
RIDER_CODE_3,
RIDER_CODE_4,
RIDER_CODE_5,
RIDER_CODE_6,
RIDER_CODE_7,
RIDER_CODE_8,
MEDICARE_STATUS_FLG,
OTHER_STATUS_FLAG,
HIRE_DATE,
ELIG_STATUS,
PREM_OVERRIDE_STEP,
PREM_OVERRIDE_AMT,
PREM_OVERRIDE_CODE,
SEQ_PROV_ID,
IPA_ID,
PANEL_ID,
SEQ_PROV_2_ID,
SECURITY_CODE,
INSERT_DATETIME,
INSERT_USER,
INSERT_PROCESS,
UPDATE_DATETIME,
UPDATE_USER,
UPDATE_PROCESS,
USER_DEFINED_1,
SALARY,
PEC_END_DATE,
REASON_CODE,
PEC_WAIVED,
BILL_EFFECTIVE_FROM_DATE,
BILLED_THRU_DATE,
PAID_THRU_DATE,
SUBSC_DEPT,
SUBSC_LOCATION,
USE_EFT_FLG,
BENEFIT_START_DATE,
SEQ_ENROLLMENT_RULE,
MCARE_RISK_ACCRETION_DATE,
MCARE_RISK_DELETION_DATE,
MCARE_RISK_REFUSED_DATE,
COMMENTS,
USER_DEFINED_2,
USER_DEFINED_3,
RATE_TYPE,
PCPAA_OCCURRED,
PRIVACY_ON,
PCP_CHANGE_REASON,
SITE_CODE,
SEQ_SITE_ADDRESS_ID)
values
(hsd_seq_elig_hist.nextval,
C.SEQ_MEMB_ID,
C.SEQ_SUBS_ID,
C.SUBSCRIBER_ID,
C.PERSON_NUMBER,
trunc(SYSDATE),
C.TERM_DATE,
C.TERM_REASON,
C.RELATIONSHIP_CODE,
C.SEQ_GROUP_ID,
C.PLAN_CODE,
C.LINE_OF_BUSINESS,
C.RIDER_CODE_1,
C.RIDER_CODE_2,
C.RIDER_CODE_3,
C.RIDER_CODE_4,
C.RIDER_CODE_5,
C.RIDER_CODE_6,
C.RIDER_CODE_7,
C.RIDER_CODE_8,
C.MEDICARE_STATUS_FLG,
C.OTHER_STATUS_FLAG,
C.HIRE_DATE,
C.ELIG_STATUS,
C.PREM_OVERRIDE_STEP,
C.PREM_OVERRIDE_AMT,
C.PREM_OVERRIDE_CODE,
C.SEQ_PROV_ID,
C.IPA_ID,
C.PANEL_ID,
C.SEQ_PROV_2_ID,
C.SECURITY_CODE,
SYSDATE,
USER,
'TD33615',
SYSDATE,
USER,
'TD33615',
C.USER_DEFINED_1,
C.SALARY,
C.PEC_END_DATE,
C.REASON_CODE,
C.PEC_WAIVED,
C.BILL_EFFECTIVE_FROM_DATE,
C.BILLED_THRU_DATE,
C.PAID_THRU_DATE,
C.SUBSC_DEPT,
C.SUBSC_LOCATION,
C.USE_EFT_FLG,
C.BENEFIT_START_DATE,
C.SEQ_ENROLLMENT_RULE,
C.MCARE_RISK_ACCRETION_DATE,
C.MCARE_RISK_DELETION_DATE,
C.MCARE_RISK_REFUSED_DATE,
C.COMMENTS,
C.USER_DEFINED_2,
C.USER_DEFINED_3,
C.RATE_TYPE,
C.PCPAA_OCCURRED,
C.PRIVACY_ON,
C.PCP_CHANGE_REASON,
C.SITE_CODE,
C.SEQ_SITE_ADDRESS_ID);
commit_cnt := commit_cnt + 1;
if (commit_cnt = 1000) then
dbms_output.put_line('Committed updates for 1000 records.');
commit;
commit_cnt := 0;
end if;
end loop;
commit;
dbms_output.put_line('Total number of MEMBER_ELIG_HISTROY records inserted : ' ||
rec_cnt);
exception
when others then
raise_application_error(-20001,
'An error was encountered - ' || sqlcode ||
' -error- ' || sqlerrm);
end;user10305724 wrote:
I have tried both the Case statement and Decode function in Select cursor, but both the things are not working. Please define what you mean by not working even if your computer screen is near the internet we can't see it.
You should also look at the FAQ about how to ask a question
SQL and PL/SQL FAQ
Particularly *9) Formatting with {noformat}{noformat} Tags* and posting your version.
know the reason why decode is not working, I heard some where Case statement do not work with 8i.
Does this mean you are using 8i? Then scalar sub queries - selects within the select list, are not supported, along with CASE in PL/SQL.
Select DECODE(1,
* (Select 1
from hsd_prov_contract R
where R.seq_prov_id = PM.seq_prov_id
and R.line_of_business = H.line_of_business
and R.PCP_FLAG = 'Y'
and R.participation_flag = 'P'
and SYSDATE between R.EFFECTIVE_DATE AND
NVL(R.TERM_DATE,
TO_DATE('31-DEC-9999', 'DD-MON-YYYY')))*,
'Y',
'N') PAR_FLAG,
>
exception
when others then
raise_application_error(-20001,
'An error was encountered - ' || sqlcode ||
' -error- ' || sqlerrm);
http://tkyte.blogspot.com/2008/01/why-do-people-do-this.html -
Regarding case statement and decode function
Hi Experts,
I have question.....regarding case statement and decode statement....
Can you please explain me that which one will be efficient,to place in insert statement...
insert statement(
(case when ........then
case when ....then
else
end)
else
end)
or
insert statement(
case when.....then
decode(....)
else
end)
Can you people explain me which one is more efficient method?
Thanks in advance.......The are major differences to talk about in case of CASE vs DECODE, but performance wise both are pretty much the same.
Have a look at Tom's thread
Ask Tom "better performance - case or decode"
I would suggest to use CASE whenever possible. Don't worry about the performance part. -
Help in changing plsql case when to decode
Hi,
Can anyone help me to change this sql for it to use decode function instead of case when? Below is the sql code
Thanks in advance.
SELECT
parts,
weeks,
SUM(qty) qty
FROM (
SELECT
CASE
WHEN ((is_tbd = 'yes' AND is_tbd_order = 'no') OR ex_fac_date > v_asofdate + 131) THEN 'tbd'
WHEN ex_fac_date BETWEEN v_asofdate - 1 AND v_asofdate + 5 AND is_tbd = 'no' THEN 'wk1'
WHEN ex_fac_date BETWEEN v_asofdate + 6 AND v_asofdate + 12 AND is_tbd = 'no' THEN 'wk2'
WHEN ex_fac_date BETWEEN v_asofdate + 13 AND v_asofdate + 19 AND is_tbd = 'no' THEN 'wk3'
WHEN ex_fac_date BETWEEN v_asofdate + 20 AND v_asofdate + 26 AND is_tbd = 'no' THEN 'wk4'
WHEN (ex_fac_date < v_asofdate - 1) AND is_tbd = 'no' THEN 'past_due'
END weeks,
ffdate,
parts,
SUM(qty) qty
FROM
delivery)I can't use case because my oracle is 8i,Please join 21st Century
Can you help me checnge this to if then elsif insteadSQL does does contain IF statement! -
Free bumper case when and how?
Steve indicated that every iPhone 4 owner would receive a free bumper case to hopefully correct the antenna issue. When and how does one receive the case? I must admit that I have only had a few dropped calls. I live in an area that has a weak signal and my 3G had not one dropped call in 2 years!!!
Apple will offer a free iPhone 4 Bumper or other select cases to iPhone 4 customers. Details on how to order a free Bumper or case will be available soon on apple.com, details most likely this week.
-
About case when and the sql clause
Hi:
The following is my sql clause:
SELECT t.*,b.name
FROM dbtest1 t
LEFT OUTER JOIN dbtest b ON t.NO = b.empno
WHERE t.ChineseName like '%'||:ChineseName||'%' AND b.name like '%'||:name||'%'
ORDER BY t.ChineseName
The main problem is I hope to check the b.name if it is null it can be passed as NVL function, so I try using case when, but it not working.
When b.name exist in where clause, the result columns will not include the data without b.name(or implies the b.name is NULL); And that
make the result data not exactly right.
is it possible to use case when to make the following snippet implemented:
case when b.name is not NULL then b.name like '%'||:name||'%'
else b.name = NULL(b.name=b.name seems not work to parsing null data)
end
Thanks a lot.962769 wrote:
Hi:
The following is my sql clause:
SELECT t.*,b.name
FROM dbtest1 t
LEFT OUTER JOIN dbtest b ON t.NO = b.empno
WHERE t.ChineseName like '%'||:ChineseName||'%' AND b.name like '%'||:name||'%'
ORDER BY t.ChineseName
The main problem is I hope to check the b.name if it is null it can be passed as NVL function, so I try using case when, but it not working.
When b.name exist in where clause, the result columns will not include the data without b.name(or implies the b.name is NULL); And that
make the result data not exactly right.
is it possible to use case when to make the following snippet implemented:
case when b.name is not NULL then b.name like '%'||:name||'%'
else b.name = NULL(b.name=b.name seems not work to parsing null data)
end
Thanks a lot.Your question isn't very clear. But below is the statement as I make a meaning out of it.
SELECT t.*,b.name
FROM dbtest1 t
LEFT OUTER JOIN dbtest b ON t.NO = b.empno
WHERE t.ChineseName like '%'||:ChineseName||'%'
AND b.name like '%'||NVL(:name, b.name)||'%'
ORDER BY t.ChineseNameIf you want to use case, then:
SELECT t.*,b.name
FROM dbtest1 t
LEFT OUTER JOIN dbtest b ON t.NO = b.empno
WHERE t.ChineseName like '%'||:ChineseName||'%'
AND b.name like '%'|| case when b.name is not null then :name else b.name end ||'%'
ORDER BY t.ChineseNameIf this is not what you are looking for then, read {message:id=9360002} and post the relevant details with an example (Create Table script, Sample Data creation script and the Expected output of the sample data). -
Please Help: Trouble with nested CASE statement and comparing dates
Please tell me why the query below is always returning the bold null even when the start_date of OLD is greater than or equal to the start_date of NEW.
What I want to do is get the difference of the start_dates of two statuses ( Start_date of OLD - Start_date of NEW) if
1. end_date of NEW is not null
2. start_date of OLD is greater than start_date of NEW
else return null.
select id,
case when max(end_date) keep (dense_rank last order by decode(request_wflow_status,'New',1,0),start_date) is null then
null
else
case when max(decode(status,'OLD',start_date,null)) > max(decode(status,'NEW',start_date,null))
then max(decode(status,'OLD',start_date,null)) - max(decode(status,'NEW',start_date,null))
else
null
end
end result
from cc_request_status where id =1
group by id;Avinash,
Thank you for your help.. Here is a more description of my problem..
Here is a sample of data I have for a table with four columns (id,status,start_date,end_date)
What I need to do is to get difference of the start dates of the maximum available dates, if data is valid. The pseducode is as follows:
IF end_date of New status is null
THEN return null
ELSE
IF start_date of old >= start_date of new
THEN return (start_date of old - start_date of new)
ELSE return null
I used the following query but always return the bold null
select id,
(case when max(end_date) keep (dense_rank last order by decode(status,'new',1,0),start_date) is null then
null
else
(case when max(decode(status,'old',start_date,null)) >=
max(decode(status,'new',start_date,null))
then max(decode(status,'old',start_date,null)) - max(decode(status,'new',start_date,null))
else
null
end)
end) result
from tbl where id =1
Based on the below sample, I expected to get the following result; 14-Mar-07 - 16-Feb-07 which is the difference of the maximum start_dates of the two statuses. However the query is not working.. Please help me.. Thank you..
Id Status start_date end_date
1 new 03-Feb-07 07-Feb-07
1 new 16-Feb-07 21-Feb-07
1 old '10-Mar-07 12-Mar-07
1 old '14-Mar-07 16-Mar-07 -
Please advise how can i use the following logic to create a case-when and incluse all the desired results in one select from table t1.
IF upper(prd) in ('AB','AC','AE')
then
If acc2 >= 0 then
Cur1 :=ccy2
Cur2 :=ccy1
amt:=acc2
elsif
acc2 < 0 then
Cur1 :=ccy1
Cur2 :=ccy2
IF acc1 IS NOT NULL then
amt := acc1
else
amt := -1 * r1 * acc2
End if;
End if;
elsif
upper(prd) IN ('AZ') then
If acc2 >= 0 then
Cur1 :=ccy2;
Cur2:=ccy1;
amt := r4;
elsif acc2 < 0 then
Cur1 =ccy1
Cur2=ccy2
if r3 IS NOT NULL THEN
amt := r3
else
amt=-1 * r2 * r4;
End If;
END If;
End if;Hi,
Here's another way:
SELECT CASE
WHEN UPPER (prd) NOT IN ('AB', 'AC', 'AE', 'AZ')
THEN NULL
WHEN acc2 >= 0 THEN ccy2
WHEN acc2 < 0 THEN ccy1
END AS cur1
, CASE
WHEN UPPER (prd) NOT IN ('AB', 'AC', 'AE', 'AZ')
THEN NULL
WHEN acc2 >= 0 THEN ccy1
WHEN acc2 < 0 THEN ccy2
END AS cur2
, CASE
WHEN UPPER (prd) IN ('AB', 'AC', 'AE')
THEN CASE
WHEN acc2 >= 0 THEN acc2
WHEN acc2 < 0 THEN NVL ( acc1
, -r1 * acc2
END
WHEN UPPER (prd) IN ('AZ')
THEN CASE
WHEN acc2 >= 0 THEN r4
WHEN acc2 < 0 THEN NVL ( r3
, -r2 * r4
END
END AS amt
...Note that the expression for amt shows how to use nested CASE statements. That can be very useful in CASE statements where the conditions are very complicated. However, I do not think that is situation here: I find Ben's suggestion (WHEN x AND y THEN ... WHEN x AND NOT y THEN ...) clearer for computing amt. -
Data missing in Crosstab report with calculated field via case when
I use the Oracle 10g discoverer. When I create a crosstab report with a calculate field via function case when and put it as datapoint, some of the data is not showing. I tried to not use the case when function for the calculate field, then all the data show.
I really do not the reason. Could anybody help me out with many thanks?Let me explain more.
I have the original data below.
Work order, Team, Hours_worked, date
800001, S1, 5, 2012/01/01
800001, S1, 15, 2012/01/10
800001, S2, 4, 2012/01/04
800002, S1, 3, 2012/01/15
There are multipul records for the same work order, team on the same day or different.
Finally I want to set the start date and end date as the parameter to create a crosstab report format like
start date>=2012/01/01 and close date<=2012/01/05
Team
Total hours total hours within the range
Work order S1 S2 ... S1 S2 ...
800001 20 4 5 4
800002 3 0 0 0
in order to do it, I create two parameters independently start date and close date. Then I create a calculate field hours_worked_withinrange via
Case when date>=:start date and date<=:end date then Hours_worked else 0 end
This calculated field is correct in the tabular format report in discoverer desktop. But when I duplicate the list as crosstab. Some data is missing in crosstab data point.
I checked once I change the calculate field defination not use case when. (For example, C1=hours_worked*2). Then everything runs correct. -
SELECT v_startdate, v_enddate,
(CASE WHEN SYSDATE BETWEEN v_startdate AND v_enddate THEN
‘active’
ELSE
‘inactive’
END) status
FROM correction_tab;
Could you kindly guide us as to how can we use DECODE and get the desired output.
Thanks,
Rami Reddy.You can, like this.
However, the CASE seems a lot clearer to me so why bother with a DECODE?
sql> with correction_tab as
2 ( select trunc(sysdate)-1 as v_startdate, trunc(sysdate) as v_enddate from dual
3 union all
4 select trunc(sysdate) as v_startdate, trunc(sysdate)+1 as v_enddate from dual
5 union all
6 select trunc(sysdate)+1 as v_startdate, trunc(sysdate)+2 as v_enddate from dual
7 )
8 SELECT v_startdate
9 , v_enddate
10 , CASE
11 WHEN SYSDATE BETWEEN v_startdate AND v_enddate
12 THEN 'active'
13 ELSE 'inactive'
14 END status
15 , decode ( sign(sysdate-v_startdate), 1, decode(sign(v_enddate-sysdate), 1, 'active', 'inactive'), 'inactive')
as status2
16 FROM correction_tab
17 /
V_STARTDA V_ENDDATE STATUS STATUS2
12-DEC-12 13-DEC-12 inactive inactive
13-DEC-12 14-DEC-12 active active
14-DEC-12 15-DEC-12 inactive inactive -
How to use case and decode to extract the data
Hello PL/SQL Gurus,
I am using Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production version
I have a table in following format -
drop table TT2;
create table TT2(College, Class,gender,status,fees) as select
'IITB','MBA','M','P',255600 from dual union all select
'IITK','MTech','M','P',300000 from dual union all select
'IITD','MBA','F','P',450000 from dual union all select
'IITKH','MBA','F','P',350000 from dual union all select
'IITC','MTech','F','P',420000 from dual union all select
'IITB','MTech','M','P',185000 from dual union all select
'IITC','MTech','M','P',235000 from dual union all select
'IITD','MBA','F','F',175000 from dual union all select
'IITM','MBA','M','F',257000 from dual union all select
'IITKH','MTech','F','P',335000 from dual union all select
'IITD','MBA','F','P',540335 from dual union all select
'IITC','MBA','F','F',125089 from dual union all select
'IITD','MTech','M','P',290756 from dual union all select
'IITM','MBA','M','P',200000 from dual union all select
'IITKH','MBA','F','F',534990 from dual union all select
'IITD','MBA','F','P',221000 from dual ;some of the extraction conditions are as following -
CASE CONDITION
College in 'IITB' and status='P'- 'WestRegion Passed'
College in 'IITC' and status='P'- 'SouthRegion Passed'
College in 'IITD' and 'IITK' and status='P' and Gender='F' - 'NothRegion Female Passed'
College not in 'IITK' and status='F' - 'Ex Kanpur Failed'
Expected output -
Region Statnding Fees
WestRegion Passed 440460
SouthRegion Passed 655000
NothRegion Female Passed 1386335
Ex Kanpur Failed 1092079SQL Used
I am using the following query which only make sure of case but this is not how i want the output , if i try to use the case within decode then how to work on this -
SELECT (CASE WHEN College in ('IITB') and status='P' then sum(fees) else 0 end) WP,
(case when College in ('IITC') and status='P' then sum(fees) else 0 end) SP,
(case when College in ('IITD','IITK') and gender='F' and status='P' then sum(fees) else 0 end) NFP,
(case when College in ('IITK') and status='F' then sum(fees) else 0 end) ExKF
FROM
TT2
GROUP BY College, Class,gender,statususer555994 wrote:
Thank you so much jeneesh i am really thankful to you ...vov.
one more query in case if any of the selection don't have the output data , then values will be displayed like -One way..
with t as
--"Add all your descriptions
(select 'WestRegion Passed' region_standing from dual union all
select 'SouthRegion Passed' region_standing from dual union all
select 'NothRegion Female Passed' region_standing from dual union all
select 'Ex Kanpur Failed' region_standing from dual)
select region_standing,sum(fees) fees
from (
(SELECT CASE WHEN College in ('IITB') and status='P'
then 'WestRegion Passed'
when College in ('IITC') and status='P'
then 'SouthRegion Passed'
when College in ('IITD','IITK') and gender='F' and status='P'
then 'NothRegion Female Passed'
when College in ('IITK') and status='F'
then 'Ex Kanpur Failed'
else 'Others' end region_standing,
sum(fees) fees
FROM TT2
GROUP BY CASE WHEN College in ('IITB') and status='P'
then 'WestRegion Passed'
when College in ('IITC') and status='P'
then 'SouthRegion Passed'
when College in ('IITD','IITK') and gender='F' and status='P'
then 'NothRegion Female Passed'
when College in ('IITK') and status='F'
then 'Ex Kanpur Failed'
else 'Others' end
union all
select region_standing,0
from t
group by region_standing;
REGION_STANDING FEES
Others 2567835
NothRegion Female Passed 1211335
WestRegion Passed 440600
Ex Kanpur Failed 0
SouthRegion Passed 655000
{code}
Edited by: jeneesh on Nov 5, 2012 5:07 PM -
Difference between CASE and DECODE
Hi All,
Could you please explain me the basic differences between the CASE and DECODE, which performs fast...?
DECODE is Oracle one, and the CASE is ANSI standard.
As per my knowledge, CASE is a statement and DECODE is a function which was defined in the Standard package.
If we use DECODE, the package has to load first, so it will take a little longer than the CASE. CASE is a simple statement which is ANSI standard.
We can use the CASE in the where clause and can not use the DECODE in the where clause.
Please clarify me and correct me if anything wrong.
Thanks,IMO, the main important point is the way CASE and DECODE handles NULL
SQL> select ename,comm,
2 decode(comm,300,'A',null,'B','C') dcd,
3 case comm when 300 then 'A'
4 when null then 'B'
5 else 'C'
6 end cs
7 from emp;
ENAME COMM D C
SMITH B C --"DECODE treats NULL=NULL. But for CASE, NULL is not equal to "another" NULL
ALLEN 300 A A
WARD 500 C C
JONES B C
MARTIN 1400 C C
BLAKE B C
CLARK B C
SCOTT B C
KING B C
TURNER 0 C C
ADAMS B C
JAMES B C
FORD B C
MILLER B C
14 rows selected.
{code}
Edited by: jeneesh on Jun 3, 2013 1:13 PM
Note: in CASE, you should use IS NULL
{code}
case when comm=300 then 'A'
when comm is null then 'B'
else 'C'
end
{code} -
Case and decode - nested functions
Can i use a case statement inside a decode or nvl2 function?
eg.
decode(col_a,'ABC',
case when col_b in (1,2) then 'CCC' else when col_b in (3,4,5) then 'XXX' end
, 'NA')
Can i use a function like this basically, can i nest a case statement within a decode or nvl2 function?
I have used nested decodes and nvls and nested case statements but not combined both so far.... so need an opinion...
Thanks.
Edited by: user254668 on Apr 30, 2010 1:35 PMuser254668 wrote:
Can i use a case statement inside a decode or nvl2 function? Absolutely. Just get rid of that else:
decode(col_a,'ABC',
case when col_b in (1,2) then 'CCC' when col_b in (3,4,5) then 'XXX' end
, 'NA') SY. -
Hi,
I am running below statement.
create table test_abc
(cnt number,
outcome varchar2(20));
insert into test_abc values(1,'EXISTINGDOMFUELCUST');
insert into test_abc values(1,'Answer Phone');
insert into test_abc values(1,'Answer Phone');
insert into test_abc values(1,'Answer Phone');
insert into test_abc values(1,'Answer Phone');
insert into test_abc values(1,'Answer Phone');
commit;
Now I used case and decode
select count(*),ot from (
select (case when outcome ='EXISTINGDOMFUELCUST' then 'Existing Domestic Fuel Customer'
when outcome='Answer Phone' then 'Answer Phone'
when outcome='Answer Phone' then 'Answer Machine' end) ot
from test_abc)
group by ot;
select count(*),decode(outcome,'EXISTINGDOMFUELCUST','Existing Domestic Fuel Customer','Answer Phone',
'Answer Phone','Answer Phone','Answer Machine')
from test_abc
group by decode(outcome,'EXISTINGDOMFUELCUST','Existing Domestic Fuel Customer','Answer Phone',
'Answer Phone','Answer Phone','Answer Machine')
and getting same output
cnt ot
5 Answer Phone
1 Existing Domestic Fuel Customer
but I want output should be
cnt ot
5 Answer Phone
5 Answer Machine
1 Existing Domestic Fuel Customer
please suggestIndra Budiantho wrote:
/* Formatted on 8/28/2012 3:10:28 PM (QP5 v5.139.911.3011) */
SELECT CASE
WHEN outcome = 'EXISTINGDOMFUELCUST'
THEN
'Existing Domestic Fuel Customer'
WHEN outcome = 'Answer Phone'
THEN
'Answer Phone'
END
outcome,
COUNT (*)
FROM test_abc
GROUP BY outcome
union
SELECT CASE
WHEN outcome = 'EXISTINGDOMFUELCUST'
THEN
'Existing Domestic Fuel Customer'
WHEN outcome = 'Answer Phone'
THEN
'Answer Machine'
END
outcome,
COUNT (*)
FROM test_abc
where outcome = 'Answer Phone'
GROUP BY outcomeo/p
Answer Machine 5
Answer Phone 5
Existing Domestic Fuel Customer 1This can be simplified..
Also, UNION will unnecessarily do a distinct operation.
SELECT CASE
WHEN outcome = 'EXISTINGDOMFUELCUST'
THEN
'Existing Domestic Fuel Customer'
WHEN outcome = 'Answer Phone'
THEN
'Answer Phone'
END
outcome,
COUNT (*)
FROM test_abc
GROUP BY outcome
union all
SELECT 'Answer Machine'
outcome,
COUNT (*)
FROM test_abc
where outcome = 'Answer Phone'
GROUP BY outcome -
Regarding case and decode.
Hi !
I need to return all the employess, and indicate with "YES" or "NO" wherther they receive a commission.
I am trying to get this by using CASE and DECODE but result is comming different.
USING DECODE:
SELECT ename,
sal,
DECODE(comm,NULL,'NO',
'YES') COMM
FROM emp;
USING CASE:
SELECT ename,
sal,
CASE comm
WHEN NULL THEN 'NO'
ELSE 'YES'
END
FROM emp;
DECODE is returning actual data(comm is NO where comm is null and comm is YES where comm is not null). Where as CASE is retunring YES for all employees commission even comm is null..
I am not able to understand where I was wrong.
Can any one help me out?
Thanks
RajeshHi Rajesh,
I tried to solve your query this way, check if it serves the purpose
/***Using Decode***/
SELECT EMPLOYEE_ID, DECODE(NVL(COMMISSION_PCT, 0), 0, 'NO', 'YES')
FROM EMPLOYEES
/****Using Case***/
SELECT EMPLOYEE_ID, CASE NVL(COMMISSION_PCT, 0)
WHEN 0 THEN 'NO'
ELSE 'YES'
END CASE
FROM EMPLOYEES
/
Maybe you are looking for
-
Photos and Albums have Disappeared
I appreciate that there seems to have been some problems with iPhoto 09 and Snow Leopard. I have been successfully using iPhoto 09 since I installed Snow Leopard without any problems. I purchased Snow Leopard immediately after it was launched. I back
-
Getting the name of outer class in an inner class
Hi, I have a private inner class, something like this: public class OuterClass extends AnotherClass { public OuterClass() { supre(); private class innerClass1 extends SomeotherClass { protected void someMethod() { // how to get the name of outer clas
-
HT202213 Can't get the correct sharing
Hi there! I recently bought a new PC and wanted to transfer my music collection from my old laptop to my new one. When ever I try to do home sharing, no matter what I do, it only gives me the option to transfer from my new PC to my old one. I have a
-
Hi, we have applied Patch3 on our Forms Environment. Now the menu background color has changed from green to gray (colorScheme is "teal" in formsweb.cfg). This behavior isn't documented and our customer want's to get the green menubar back... Any hin
-
Can't install Photoshop CC 64bit
I just signed up for Adobe Creative Cloud. Dreamweaver and Flash initiated flawlessly - got a license agreement screen, agreed to terms and they launched. When I did this with Photoshop 64bit, after the license screen i got this message "Cannot comp