| Oracle case statment solution [message #637128] |
Mon, 11 May 2015 05:55  |
 |
purnima1
Messages: 79 Registered: June 2014
|
Member |
|
|
Hi All,
CAn somebody help in simplifying the below mentioned code.Actully if you find we are calculating same thing again and again in when clause and As per my understanding it will calculate the value for all the when clause till the time it satify any of when clause or reaches till else.
1) please correct if my this understanding is wrong.
2) How can i avoid this repetative calculation in when clause again and again.
Thanks in advance
declare
TRAD_MATURITY_DATE date := '21-feb-2015';
STRT_DATE date :='20-feb-2015';
p_val varchar2(10);
begin
select
case when ((extract(YEAR from TRAD_MATURITY_DATE) - extract(YEAR from STRT_DATE))*12 +
( extract(MONTH from TRAD_MATURITY_DATE) -extract(MONTH from STRT_DATE) )+
(case when extract(DAY from TRAD_MATURITY_DATE )> extract(DAY from STRT_DATE) then 1 else 0 end ) ) between 1 and 3 then 'OT01'
when ((extract(YEAR from TRAD_MATURITY_DATE) - extract(YEAR from STRT_DATE))*12 +
( extract(MONTH from TRAD_MATURITY_DATE) -extract(MONTH from STRT_DATE) )+
(case when extract(DAY from TRAD_MATURITY_DATE )> extract(DAY from STRT_DATE) then 1 else 0 end ) ) between 4 and 7 then 'OT02'
else
'Unknown' end into p_val from dual ;
dbms_output.put_line(p_val);
end;
|
|
|
|
| Re: Oracle case statment solution [message #637129 is a reply to message #637128] |
Mon, 11 May 2015 06:00   |
Lalit Kumar B
Messages: 3174 Registered: May 2013 Location: World Wide on the Web
|
Senior Member |
|
|
purnima1 wrote on Mon, 11 May 2015 16:25
TRAD_MATURITY_DATE date := '21-feb-2015';
STRT_DATE date :='20-feb-2015';
'21-feb-2015' is a string and NOT a DATE.
Regarding helping you with the query, you need to explain in words, what actually you are trying to achieve with the query? Can you post the sample data? Please read how to post a test case.
|
|
|
|
|
|
| Re: Oracle case statment solution [message #637133 is a reply to message #637130] |
Mon, 11 May 2015 08:04   |
cookiemonster
Messages: 13975 Registered: September 2008 Location: Rainy Manchester
|
Senior Member |
|
|
As it stands the second WHEN in the CASE statement is completely pointless.
1 and 0 can never be between 4 and 7.
You can use nested queries or WITH clauses to avoid doing calculations multiple times.
Or if (as your example implies) you aren't querying the data from anywhere then you can just do the calculation in PL/SQL and store it in a variable as Michel already suggested. If you really are just using dual then I'd just use an IF statement in PL/SQL and skip the query entirely.
|
|
|
|
|
|
|
|
| Re: Oracle case statment solution [message #637167 is a reply to message #637160] |
Tue, 12 May 2015 03:25  |
cookiemonster
Messages: 13975 Registered: September 2008 Location: Rainy Manchester
|
Senior Member |
|
|
I'd be very surprised if an oracle tool prevented you from using any standard SQL clause, but if OWB really doesn't allow WITH (I've never used it so I don't know) then use an inline view as Michel suggested.
This:
(extract(YEAR from TRAD_MATURITY_DATE) - extract(YEAR from STRT_DATE))*12 +
( extract(MONTH from TRAD_MATURITY_DATE) -extract(MONTH from STRT_DATE) )
Can be simplified to this:
floor(months_between(TRAD_MATURITY_DATE, STRT_DATE))
|
|
|
|