Case when help
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.
Similar Messages
-
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! -
Need help converting Excel IF statement into CASE WHEN statement for Oracle
Hi,
Hope someone can help I have tried various ways of getting this to work, to no avail!
Bascially I have some figures that are minus figures, and I need to add them together to get a movement figure, but I need to treat the minus figures as minus, if that makes sense, rather than the usual... a minus and a minus makes a plus.
For example:- Budget Figure = -1% and Actual Figure = -68% so the movement needs to be -69%.
The IF statement I have been using in Excel is the following:-
=IF(FO110<0,(FP110-(IF(FO110=0,1,FO110)*-1)),FP110-IF(FO110=0,1,FO110))
Which when using the figures as above = -69%
Cell FO = The Budget Figure
Cell FP = The Actual Figure
However, when I created the CASE statement in Oracle, the figure in the query comes back as -0.67, which is oviously not what I want to happen when both actual and budget are minus figures; however when they are a minus and a plus, it works perfectly fine.
Any help on this would be most appreciated!
Kind regards,
AnnmarieHappy I did'n mess something up :)
Nevertheless, don't show it too much around because
case when budget < 0
then actual - case when budget = 0 /* will never happen */
then 1 /* will never happen */
else budget
end * (-1) /* -budget * (-1) remains only */
else actual - case when budget = 0
then 1
else budget
end
endso at least try the following (if case is more readable as decode for you). Let's hope it works as I don't have a Database at hand
case when budget < 0
then actual + budget
else actual - case when budget = 0
then 1
else budget
end
end Regards
Etbin -
Hello Cisco Team/Everyone,
We are facing one major hurdle we need to configure a case when a email should be sent to business if the feed is not received within SLA.
At present what we can configure is if feed is being received within SLA then an email will be sent informing about the timely delievery of feed and if we receive feed after SLA then also an email will be sent to business informing that feed received late but if we haven't received the feed within SLA then please let us know how to configure Email Action ? ( informing that we haven't received the feed within SLA )
Thanks.You have muliple solutions so we probably need more details
At a mininum you would want 2 different email events with different triggers and 2 different email actions
You may or may not have 2 different jobs
How we decided to handle:
We decided to create and ALERT Job Group for our SLA notifications/alerts
(you can make it its own parent or integreate with another Job Group depeending on what makes sense to you)
We organized the next level by time windows in military time
Alert\
\0000_0259 <-- alerts that trigger between midnight and 3am (they line up top to bottom that way and visually progress)
If this is a file based alert you create a dependecy on the file not existing
Next put a Time Window on it lets say start at 12:15am and end at 1:00 am
Here's the counter intuitive part:
You create an event to send an email to business on job completed normally that the SLA was breached
if the file didn't exist by 1am then this executes and completes normally
We use powershell to write-output of text we want to send for that particular business unit
We use in email action
You create an event to skip the job if job not ready by the end of its time window
if the file existed then the depdendcy is not statisfied and job essentailly is not ready by end of time window 1am
Operations teams consider "skipped" a "OK" status since we do it on purpose
Other options
You could also use exit codes to do similar things, if your process that is evaluating the SLA is produce different exit codes, Then you can apply different events to the exit codes
Hope that helps!
Marc -
SQL - Select Help - Case When? Return Value from Second Table?
Hi - next to folks on this board I am probably somewhere between a Beginner and an Intermediate SQL user.
Ive been using a case when statement in plsql to find "all those who's status in any program was cancelled during a specific time, but have become or are still active in a second program"
So, Im effectively trying to return a value from a second table in a case when, but its not liking anthing other than a declared text like 'Yes' or 'No'.
Here is the select statement - is there another way to do this where I can get the results I need?
case when pp.party_id in (select pp1.party_id -- Cancelled clients Active in another program
from asa.program_participation pp1,
asa.curr_prog_participation_status cpps1
where pp1.program_participation_id = cpps1.program_participation_id
and pp1.party_id = pp.party_id
and cpps1.code_value = 'ACT')
then 'Yes' else 'No' end as Active_in_Other_Prg
So - in place of 'Yes' i basically want the program that they are active in or pp1.program_id, else Null
It is possible that the client can be active in more than one program as well.
Any assistance is greatly appreciated, i explored with if's and decodes but I cant get anything to work.
BateseySounds like an outer join. See ora doc: Joins
select p.*
, q.party_id
, q.program_id
from table_with_party_id p
, ( select pp1.party_id -- Cancelled clients Active in another program
, pp1.program_id
from asa.program_participation pp1,
asa.curr_prog_participation_status cpps1
where pp1.program_participation_id = cpps1.program_participation_id
and pp1.party_id = pp.party_id
and cpps1.code_value = 'ACT') q
where p.party_id = q.party_id ( +)
Note: In the example above there shoudn't be a space between the ( and +), but the forum software automagically converts this to
The outer join will link show all records from the p table and only records from q if the party_id matches, ie q.party_id and q.program_id will be null if there is no match.
edit: added program_id -
Create global temporary table d_desc1
( discrep_id NUMBER
,desc_1 VARCHAR2(110)
Create global temporary table d_desc2
( discrep_id NUMBER
,desc_2 VARCHAR2(110)
INSERT INTO
d_desc1
SELECT distinct
d.discrep_id
,NVL(dd.discrep_desc,'') as desc_1
FROM
discrep_ d
,discrep_desc_ dd
WHERE
d.discrep_id = dd.discrep_id
AND dd.DISCREP_DESC_SEQUENCE = 1
AND dd.html_ind <> 'Y'
AND dd.discrep_desc <> '<br>'
AND dd.discrep_desc NOT LIKE '<font Class=%'
AND d.discrep_id in (915606255, 879868258, 826503010, 824812060)
AND d.discrep_date >= '01-jan-2009'
AND d.discrep_date <= to_date('31-jan-2009')+1;
INSERT INTO
d_desc2
SELECT distinct
d.discrep_id
,case when d1.discrep_id = dd.discrep_id then d1.desc_1 || '' || dd.discrep_desc
when d1.discrep_id <> dd.discrep_id then dd.discrep_desc as desc_2
FROM
discrep_ d
,discrep_desc_ dd
WHERE
d.discrep_id = dd.discrep_id
AND dd.DISCREP_DESC_SEQUENCE = 2
AND dd.html_ind <> 'Y'
AND dd.discrep_desc <> '<br>'
AND dd.discrep_desc NOT LIKE '<font Class=%'
AND d.discrep_id IN (915606255, 879868258, 826503010, 824812060)
AND d.discrep_date >= '01-jan-2009'
AND d.discrep_date <= to_date('31-jan-2009')+1;
CREATE TABLE DISCREP_
( DISCREP_ID NUMBER,
DISCREP_DATE DATE, )
CREATE TABLE DISCREP_DESC_
( DISCREP_ID NUMBER,
DISCREP_DESC_SEQUENCE NUMBER ,
DISCREP_DESC_DATE DATE ,
DISCREP_DESC NVARCHAR2(100) ,
HTML_IND CHAR(1 BYTE) ,
PERSON_ID NUMBER
discrep_desc for a given discrep_id can have several discrep_desc_sequence for the same date. I need to put them on one line and be able to query it by discrep_id to update a column in another table with the data. I don't plan to use global temporary tables in my procedure for this but to test it for now I am using them. the case statement is giving me d1.desc_1 || '' || dd.discrep_desc and dd.discrep_desc for a given discrep_id instead of just dd.discrep_desc if the discrep_id isn't in d_desc1 and d1.desc_1 || '' || dd.discrep_desc if the discrep_id is in both tables. the first insert should give you 3 rows and the second insert should only give you 3. 826503010 is only in d_desc2 and 879868258 is only in d_desc1. if you know of a loop or anything else that might work better please give me an idea how to write it. I am using oracle 10g.
thanks.figured out myself. Thanks.
-
Using case when statement in the select query to create physical table
Hello,
I have a requirement where in I have to execute a case when statement with a session variable while creating a physical table using a select query. let me explain with an example.
I have a physical table based on a select table with one column.
SELECT 'VALUEOF(NQ_SESSION.NAME_PARAMETER)' AS NAME_PARAMETER FROM DUAL. Let me call this table as the NAME_PARAMETER table.
I also have a customer table.
In my dashboard that has two pages, Page 1 contains a table with the customer table with column navigation to my second dashboard page.
In my second dashboard page I created a dashboard report based on NAME_PARAMETER table and a prompt based on customer table that sets the NAME_ PARAMETER request variable.
EXECUTION
When i click on a particular customer, the prompt sets the variable NAME_PARAMETER and the NAME_PARAMETER table shows the appropriate customer.
everything works as expected. YE!!
Now i created another table called NAME_PARAMETER1 with a little modification to the earlier table. the query is as follows.
SELECT CASE WHEN 'VALUEOF(NQ_SESSION.NAME_PARAMETER)'='Customer 1' THEN 'TEST_MART1' ELSE TEST_MART2' END AS NAME_PARAMETER
FROM DUAL
Now I pull in this table into the second dashboard page along with the NAME_PARAMETER table report.
surprisingly, NAME_PARAMETER table report executes as is, but the other report based on the NAME_PARAMETER1 table fails with the following error.
Error Codes: OPR4ONWY:U9IM8TAC:OI2DL65P
State: HY000. Code: 10058. [NQODBC] [SQL_STATE: HY000] [nQSError: 10058] A general error has occurred. [nQSError: 16001] ODBC error state: S1000 code: 1756 message: [Oracle][ODBC][Ora]ORA-01756: quoted string not properly terminated. [nQSError: 16014] SQL statement preparation failed. (HY000)
SQL Issued: SET VARIABLE NAME_PARAMETER='Novartis';SELECT NAME_PARAMETER.NAME_PARAMETER saw_0 FROM POC_ONE_DOT_TWO ORDER BY saw_0
If anyone has any explanation to this error and how we can achieve the same, please help.
Thanks.Hello,
Updates :) sorry.. the error was a stupid one.. I resolved and I got stuck at my next step.
I am creating a physical table using a select query. But I am trying to obtain the name of the table dynamically.
Here is what I am trying to do. the select query of the physical table is as follows.
SELECT CUSTOMER_ID AS CUSTOMER_ID, CUSTOMER_NAME AS CUSTOMER_NAME FROM 'VALUEOF(NQ_SESSION.SCHEMA_NAME)'.CUSTOMER.
The idea behind this is to obtain the data from the same table from different schemas dynamically based on what a session variable. Please let me know if there is a way to achieve this, if not please let me know if this can be achieved in any other method in OBIEE.
Thanks. -
Case When Statement and ORA:01722 Invalid number error
Hi folks, I have posted this under another heading as well under E-business suite so apologies if some you have already seen it but I would really appreciate some help on this one. Any suggestions are most welcome.
We are trying to put together a calculation that returns the number of days absent an individual has had in a given time period. We need to cater for those absences that started before the period and are closed during it, absence that start during the period and end after it, and those that open and close within it.
The period is always a rolling 6 months from sysdate.
This is the calc we have come up with so far which works for some people but we get the invalid number error if the absence includes a half day - so 0.5, 1.5,etc.
This is probably over complicated but we are not techie at all so are learning as we go!
We are using the HRMS - Administration - Oracle Human Resources (Core) business area in 10G and the Absence Attendance and Person folders.
SUM(TO_NUMBER(NVL(( CASE WHEN Absence Attendance.Actual Start Date < TO_DATE(ADD_MONTHS(SYSDATE,-6),'DD-Mon-YYYY') THEN ( CASE WHEN Absence Attendance."Actual End Date" > SYSDATE THEN ( WORKING_DAYS_BETWEEN(TO_DATE(ADD_MONTHS(SYSDATE,-6),'DD-Mon-YYYY'),SYSDATE) ) ELSE ( CASE WHEN ( CASE WHEN TO_DATE(ADD_MONTHS(SYSDATE,-6),'DD-Mon-YYYY') >= Absence Attendance.Actual Start Date THEN ( WORKING_DAYS_BETWEEN(TO_DATE(ADD_MONTHS(SYSDATE,-6),'DD-Mon-YYYY'),Absence Attendance."Actual End Date") ) ELSE ( CASE WHEN ( WORKING_DAYS_BETWEEN(Absence Attendance.Actual Start Date,Absence Attendance."Actual End Date") ) IS NULL THEN ( WORKING_DAYS_BETWEEN(Absence Attendance.Actual Start Date,SYSDATE) ) ELSE ( CASE WHEN SYSDATE <= Absence Attendance."Actual End Date" THEN ( WORKING_DAYS_BETWEEN(Absence Attendance.Actual Start Date,SYSDATE) ) ELSE ( WORKING_DAYS_BETWEEN(Absence Attendance.Actual Start Date,Absence Attendance."Actual End Date") ) END ) END ) END ) IS NULL THEN ( WORKING_DAYS_BETWEEN(TO_DATE(ADD_MONTHS(SYSDATE,-6),'DD-Mon-YYYY'),SYSDATE) ) ELSE ( CASE WHEN TO_DATE(ADD_MONTHS(SYSDATE,-6),'DD-Mon-YYYY') >= Absence Attendance.Actual Start Date THEN ( WORKING_DAYS_BETWEEN(TO_DATE(ADD_MONTHS(SYSDATE,-6),'DD-Mon-YYYY'),Absence Attendance."Actual End Date") ) ELSE ( CASE WHEN ( WORKING_DAYS_BETWEEN(Absence Attendance.Actual Start Date,Absence Attendance."Actual End Date") ) IS NULL THEN ( WORKING_DAYS_BETWEEN(Absence Attendance.Actual Start Date,SYSDATE) ) ELSE ( CASE WHEN SYSDATE <= Absence Attendance."Actual End Date" THEN ( WORKING_DAYS_BETWEEN(Absence Attendance.Actual Start Date,SYSDATE) ) ELSE ( WORKING_DAYS_BETWEEN(Absence Attendance.Actual Start Date,Absence Attendance."Actual End Date") ) END ) END ) END ) END ) END ) ELSE ( CASE WHEN ( CASE WHEN TO_DATE(ADD_MONTHS(SYSDATE,-6),'DD-Mon-YYYY') >= Absence Attendance.Actual Start Date THEN ( WORKING_DAYS_BETWEEN(TO_DATE(ADD_MONTHS(SYSDATE,-6),'DD-Mon-YYYY'),Absence Attendance."Actual End Date") ) ELSE ( CASE WHEN ( WORKING_DAYS_BETWEEN(Absence Attendance.Actual Start Date,Absence Attendance."Actual End Date") ) IS NULL THEN ( WORKING_DAYS_BETWEEN(Absence Attendance.Actual Start Date,SYSDATE) ) ELSE ( CASE WHEN SYSDATE <= Absence Attendance."Actual End Date" THEN ( WORKING_DAYS_BETWEEN(Absence Attendance.Actual Start Date,SYSDATE) ) ELSE ( WORKING_DAYS_BETWEEN(Absence Attendance.Actual Start Date,Absence Attendance."Actual End Date") ) END ) END ) END ) IS NULL THEN ( WORKING_DAYS_BETWEEN(TO_DATE(ADD_MONTHS(SYSDATE,-6),'DD-Mon-YYYY'),SYSDATE) ) ELSE ( CASE WHEN TO_DATE(ADD_MONTHS(SYSDATE,-6),'DD-Mon-YYYY') >= Absence Attendance.Actual Start Date THEN ( WORKING_DAYS_BETWEEN(TO_DATE(ADD_MONTHS(SYSDATE,-6),'DD-Mon-YYYY'),Absence Attendance."Actual End Date") ) ELSE ( CASE WHEN ( WORKING_DAYS_BETWEEN(Absence Attendance.Actual Start Date,Absence Attendance."Actual End Date") ) IS NULL THEN ( WORKING_DAYS_BETWEEN(Absence Attendance.Actual Start Date,SYSDATE) ) ELSE ( CASE WHEN SYSDATE <= Absence Attendance."Actual End Date" THEN ( WORKING_DAYS_BETWEEN(Absence Attendance.Actual Start Date,SYSDATE) ) ELSE ( Absence Attendance.Duration Days ) END ) END ) END ) END ) END ),( DAYS_BETWEEN(Absence Attendance.Actual Start Date,Absence Attendance."Actual End Date") )),'999999990D00'))Hi,
It could be that this is because you are using SYSDATE which contains the time as a fraction rather than TRUNC(SYSDATE) which just contains the current time. It could be that your working_dates_between raises this error.
However, your formula is far more complicated than it needs to be.
Firstly, you want to look at the date window ADD_MONTHS(TRUNC(SYSDATE), -6) to TRUNC(SYSDATE). Then you want to look at the portion of the absence that falls in the date window. This is GREATEST(Absence Attendance.Actual Start Date, ADD_MONTHS(TRUNC(SYSDATE), -6)) to LEAST(NVL(Absence Attendance."Actual End Date",TRUNC(SYSDATE)), TRUNC(SYSDATE)). You may need to add 1 to the absence end date because this is the last day of their absence rather than the date they return. It depends how you calculate the days between the start and end
date of the absence. You can create calculations for the start and end date of the absences within the 6 months time window. Create calculation AbsenceStart as
GREATEST(Absence Attendance.Actual Start Date, ADD_MONTHS(TRUNC(SYSDATE), -6))
and AbsenceEnd as
LEAST(NVL(Absence Attendance."Actual End Date",TRUNC(SYSDATE)), TRUNC(SYSDATE))
Then you need to only pick up absence that a part of the absence in your 6 month date window. You can use a condition in the workbook or a condition in a case statement to do this. You then need to calculate the difference between these dates and SUM all the values.
SUM(CASE WHEN AbsenceEnd >= AbsenceStart THEN WORKING_DAYS_BETWEEN(AbsenceStart, AbsenceEnd) END)
That's it. Not so complicated after all.
Rod West -
Case When Statement and ORA:1722 Invalid number error
Sorry I posted this in the wrong forum - I have the answer now
Cheers
HELP!!!
We are trying to put together a calculation that returns the number of days absent an individual has had in a given time period. We need to cater for those absences that started before the period and are closed during it, absence that start during the period and end after it, and those that open and close within it.
The period is always a rolling 6 months from sysdate.
This is the calc we have come up with so far which works for some people but we get the invalid number error if the absence includes a half day - so 0.5, 1.5,etc.
This is probably over complicated but we are not Techie at all so are learning as we go! We are using the HRMS - Administration - Oracle Human Resources (Core) business area in 10G and the Absence Attendance and Person folders.
SUM(TO_NUMBER(NVL(( CASE WHEN Absence Attendance.Actual Start Date < TO_DATE(ADD_MONTHS(SYSDATE,-6),'DD-Mon-YYYY') THEN ( CASE WHEN Absence Attendance."Actual End Date" > SYSDATE THEN ( WORKING_DAYS_BETWEEN(TO_DATE(ADD_MONTHS(SYSDATE,-6),'DD-Mon-YYYY'),SYSDATE) ) ELSE ( CASE WHEN ( CASE WHEN TO_DATE(ADD_MONTHS(SYSDATE,-6),'DD-Mon-YYYY') >= Absence Attendance.Actual Start Date THEN ( WORKING_DAYS_BETWEEN(TO_DATE(ADD_MONTHS(SYSDATE,-6),'DD-Mon-YYYY'),Absence Attendance."Actual End Date") ) ELSE ( CASE WHEN ( WORKING_DAYS_BETWEEN(Absence Attendance.Actual Start Date,Absence Attendance."Actual End Date") ) IS NULL THEN ( WORKING_DAYS_BETWEEN(Absence Attendance.Actual Start Date,SYSDATE) ) ELSE ( CASE WHEN SYSDATE <= Absence Attendance."Actual End Date" THEN ( WORKING_DAYS_BETWEEN(Absence Attendance.Actual Start Date,SYSDATE) ) ELSE ( WORKING_DAYS_BETWEEN(Absence Attendance.Actual Start Date,Absence Attendance."Actual End Date") ) END ) END ) END ) IS NULL THEN ( WORKING_DAYS_BETWEEN(TO_DATE(ADD_MONTHS(SYSDATE,-6),'DD-Mon-YYYY'),SYSDATE) ) ELSE ( CASE WHEN TO_DATE(ADD_MONTHS(SYSDATE,-6),'DD-Mon-YYYY') >= Absence Attendance.Actual Start Date THEN ( WORKING_DAYS_BETWEEN(TO_DATE(ADD_MONTHS(SYSDATE,-6),'DD-Mon-YYYY'),Absence Attendance."Actual End Date") ) ELSE ( CASE WHEN ( WORKING_DAYS_BETWEEN(Absence Attendance.Actual Start Date,Absence Attendance."Actual End Date") ) IS NULL THEN ( WORKING_DAYS_BETWEEN(Absence Attendance.Actual Start Date,SYSDATE) ) ELSE ( CASE WHEN SYSDATE <= Absence Attendance."Actual End Date" THEN ( WORKING_DAYS_BETWEEN(Absence Attendance.Actual Start Date,SYSDATE) ) ELSE ( WORKING_DAYS_BETWEEN(Absence Attendance.Actual Start Date,Absence Attendance."Actual End Date") ) END ) END ) END ) END ) END ) ELSE ( CASE WHEN ( CASE WHEN TO_DATE(ADD_MONTHS(SYSDATE,-6),'DD-Mon-YYYY') >= Absence Attendance.Actual Start Date THEN ( WORKING_DAYS_BETWEEN(TO_DATE(ADD_MONTHS(SYSDATE,-6),'DD-Mon-YYYY'),Absence Attendance."Actual End Date") ) ELSE ( CASE WHEN ( WORKING_DAYS_BETWEEN(Absence Attendance.Actual Start Date,Absence Attendance."Actual End Date") ) IS NULL THEN ( WORKING_DAYS_BETWEEN(Absence Attendance.Actual Start Date,SYSDATE) ) ELSE ( CASE WHEN SYSDATE <= Absence Attendance."Actual End Date" THEN ( WORKING_DAYS_BETWEEN(Absence Attendance.Actual Start Date,SYSDATE) ) ELSE ( WORKING_DAYS_BETWEEN(Absence Attendance.Actual Start Date,Absence Attendance."Actual End Date") ) END ) END ) END ) IS NULL THEN ( WORKING_DAYS_BETWEEN(TO_DATE(ADD_MONTHS(SYSDATE,-6),'DD-Mon-YYYY'),SYSDATE) ) ELSE ( CASE WHEN TO_DATE(ADD_MONTHS(SYSDATE,-6),'DD-Mon-YYYY') >= Absence Attendance.Actual Start Date THEN ( WORKING_DAYS_BETWEEN(TO_DATE(ADD_MONTHS(SYSDATE,-6),'DD-Mon-YYYY'),Absence Attendance."Actual End Date") ) ELSE ( CASE WHEN ( WORKING_DAYS_BETWEEN(Absence Attendance.Actual Start Date,Absence Attendance."Actual End Date") ) IS NULL THEN ( WORKING_DAYS_BETWEEN(Absence Attendance.Actual Start Date,SYSDATE) ) ELSE ( CASE WHEN SYSDATE <= Absence Attendance."Actual End Date" THEN ( WORKING_DAYS_BETWEEN(Absence Attendance.Actual Start Date,SYSDATE) ) ELSE ( Absence Attendance.Duration Days ) END ) END ) END ) END ) END ),( DAYS_BETWEEN(Absence Attendance.Actual Start Date,Absence Attendance."Actual End Date") )),'999999990D00'))
Edited by: CPearce on Sep 25, 2008 8:03 AMHi,
It could be that this is because you are using SYSDATE which contains the time as a fraction rather than TRUNC(SYSDATE) which just contains the current time. It could be that your working_dates_between raises this error.
However, your formula is far more complicated than it needs to be.
Firstly, you want to look at the date window ADD_MONTHS(TRUNC(SYSDATE), -6) to TRUNC(SYSDATE). Then you want to look at the portion of the absence that falls in the date window. This is GREATEST(Absence Attendance.Actual Start Date, ADD_MONTHS(TRUNC(SYSDATE), -6)) to LEAST(NVL(Absence Attendance."Actual End Date",TRUNC(SYSDATE)), TRUNC(SYSDATE)). You may need to add 1 to the absence end date because this is the last day of their absence rather than the date they return. It depends how you calculate the days between the start and end
date of the absence. You can create calculations for the start and end date of the absences within the 6 months time window. Create calculation AbsenceStart as
GREATEST(Absence Attendance.Actual Start Date, ADD_MONTHS(TRUNC(SYSDATE), -6))
and AbsenceEnd as
LEAST(NVL(Absence Attendance."Actual End Date",TRUNC(SYSDATE)), TRUNC(SYSDATE))
Then you need to only pick up absence that a part of the absence in your 6 month date window. You can use a condition in the workbook or a condition in a case statement to do this. You then need to calculate the difference between these dates and SUM all the values.
SUM(CASE WHEN AbsenceEnd >= AbsenceStart THEN WORKING_DAYS_BETWEEN(AbsenceStart, AbsenceEnd) END)
That's it. Not so complicated after all.
Rod West -
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 WHEN functionality in BI Publisher
I have previously used Discoverer to do our reporting and am investigating the differences between the two products. I understand that its chalk and cheese but one thing I am having trouble identifying is the CASE WHEN functionality that we use Discoverer in BI Publisher.
An example of the usage for our CASE WHEN would be creating a derived column in a report to convert existing data into a more standardised format.
I imagine this is quite a simple answer and I have just missed it but could anyone out there help me with where I can input the functionality in BI publisher.
I am not the most technically gifted so any answers with simple words help :-)
Edited by: 943098 on 27-Jun-2012 03:25You can use CASE WHEN in a sql query in a data model. You can also use it in the RTF template via "if then else", choose statements etc
Take a look at section 4.10 in the BI publisher Report Designer's guide for use cases/examples: http://docs.oracle.com/cd/E23943_01/bi.1111/e22254.pdf
Thanks,
Bipuser -
hi gems...
i have a case when statement in a select clause...there are total three conditions in the case when statemnt.
now my question is if all theconditions gets matched, then what will happen???
is only first condition gets executed and rest two will be ignored or the third condition will overwrite the previous two???
please help...thanks in advance...Hello
This isn't very difficult to test....
select
CASE
WHEN 1=1 THEN
1
WHEN 1=1 THEN
2
WHEN 1=1 THEN
3
END c
FROM
dual
C
1
1 row selected.It wil return the first match.
David -
Using :case when in where clause
Hello,
I need some help with using of case statement in a where clause
Table that contains info about employees taking some coursework:
Class (values could be SAP, ORACLE, JAVA)
Code (if Class is SAP then CODE is not null; if class is any other CODE is NULL)
Start Date (date they began class not null)
End Date (date then ended the class - could be null)
Employee Level(numbers from one through five)
I need a single LOV in forms that should show Employee_Level for input class,code,date.
How to do this?
I started off with this but get 'missing statement error'
select distinct employee_level from e_course
where (
case when &class='SAP' then code ='1' and start_date > to_date('20000101','YYYYMMDD') and
end_date < to_date('20000101','YYYYMMDD')
else
null
end) order by employee_level;ThanksHi,
user469956 wrote:
But all these examples have only one condition for each case.Depending on how you count, all WHERE clauses have only one condition.
I see an example in that thread that has 6 atomic conditions, linked by AND and OR.
I need to check date & code. This is what is causing the error.Why do you want to use a CASE statement?
Why can't you put all your conditions directly iinto a WHERE clause, soemthing like this:
WHERE ( &class='SAP'
AND code ='1'
OR ( start_date > to_date('20000101','YYYYMMDD')
AND end_date < to_date('20000101','YYYYMMDD')
)I said "something like this" because I don't know what you really want to do. -
Using case when statement or decode stament in where clause
hi gems..
i have a problem in the following query..
i am trying to use case when statement in the where clause of a select query.
select cr.customer_name || ' - ' ||cr.customer_number as cust_name,
cr.salary as salary
from customer_details cr
where (case when '>' = '>' then 'cr.salary > 5000'
when '>' = '<' then 'cr.salary < 5000'
when '>' = '=' then 'cr.salary = 5000'
else null
end);
the expression in the when clause of the case-when statement will come from UI and depending on the choice i need to make the where clause.
thats why for running the query, i have put '>' in that place.
so the original query will look like this(for your reference):
select cr.customer_name || ' - ' ||cr.customer_number as cust_name,
cr.salary as salary
from customer_details cr
where (case when variable = '>' then 'cr.salary > 5000'
when variable = '<' then 'cr.salary < 5000'
when variable = '=' then 'cr.salary = 5000'
else null
end);
so, in actual case,if the user selects '>' then the filter will be "where cr.salary > 5000"
if the user selects '<' then the filter will be "where cr.salary < 5000"
if the user selects '=' then the filter will be "where cr.salary = 5000"
but i am getting the error "ORA 00920:invalid relational operator"
please help..thanks in advance..Hi,
select cr.customer_name || ' - ' ||cr.customer_number as cust_name,
cr.salary as salary
from customer_details cr
where ( v_variable = 'bigger'
and cr.salary > 5000
or ( v_variable = 'less'
and cr.salary < 5000
or ( v_variable = 'eq'
and cr.salary = 5000
)Edited by: user6806750 on 22.12.2011 14:56
For some reason I can't write in sql '<', '>', '=' -
Count (case when) not working in sql
Hi All,
I am trying to find the count of members for a perticular date range, it seems not working correclty.
Could any one help me out.
here is the query...I am getting same output for all the cases..please help
Select
t
.MemberNbr,t.patcom
count(case
when
datediff(d,
mbrs.DischargeDate,
convert(datetime,convert(varchar(12),t.specificdateofservice),112))
between
-1
and
-365
then t.MemberNbr
else 0
end)as
edvisits365daysprior
count(case
when
datediff(d,
mbrs.DischargeDate,
convert(datetime,convert(varchar(12),t.specificdateofservice),112))
between 1
and 3
then t.MemberNbr
else 0
end)as
edvisits3Days
count(case
when
datediff(d,
mbrs.DischargeDate,
convert(datetime,convert(varchar(12),t.specificdateofservice),112))
between 1
and 30
then t.MemberNbr
else 0
end)as
edvisits30Days
count(case
when
datediff(d,
mbrs.DischargeDate,
convert(datetime,convert(varchar(12),t.specificdateofservice),112))
between 1
and 60
then t.MemberNbr
else 0
end)as
edvisits60Days
count(case
when
datediff(d,
mbrs.DischargeDate,
convert(datetime,convert(varchar(12),t.specificdateofservice),112))
between 1
and 90
then t.MemberNbr
else 0
end)as
edvisits90Days
--,*--membernbr,specificdateofservice,count( membernbr)--, specificdateofservice)as visitcount--, patcom
--Into #Tmp_PPCSEDCnt1
From
stg_tbl_InPatientPrepost Mbrs
join
#Tmp_MCCSEDVisits t
on
mbrs.MemberNbr=t.MemberNbr
--where UPPER(t.MR_Line_Desc) in ('FOP EMERGENCY ROOM - HOSPITAL', 'FOP EMERGENCY ROOM Urgent Care',
--'PROF ER VISITS AND OBSERVATION CARE')
group
by t.MemberNbr,t.patcomYou may replace 0 as NULL and see how it works for you?
eg:
count(case when datediff(d, mbrs.DischargeDate,
convert(datetime,convert(varchar(12),t.specificdateofservice),112))
between -1 and -365 thent.MemberNbr else NULL end)as edvisits365daysprior
Maybe you are looking for
-
How set and get parameter for a session of a portlet (Java)
I would like get parameter and set parameter for a portlet in portal 9.02. This parameter must be availbale during the session of the portal. With Java Servlet // I retrieve my session HttpServletRequest httpreq = (HttpServletRequest)req; HttpServlet
-
Such a farse. I used to have a high opinion of BT'...
We moved house on the 28th. The day of the move, as to be expected the phone and internet was cancelled at the old house and we moved to the new house that same day. Same homehub and everything, even the same type of phone socket that has a dedicated
-
Hi, I've downloaded and installed java 1.5.0-5 and have it working fine with firefox. IE is another matter. I have both the advanced tab checked to use java and the security tab checked to use scripting but for some reason on a xp sp 2 machine ie and
-
Topic Downloading instead of viewing
Browsing the forums and I clicked on Windows 10:Will it Soar and it immediately lead me to download the topic, I'm fairly sure this is some kind of bug. I've viewed this exact topic before on Friday and I was able to view it normally. Is this a new n
-
Why does cnn or other links open in desktop view?
Every browser I've used (chrome, opera, dolphin,etc.) will open www.cnn.com in a readable, single-column, format. How come Firefox opens in a full desktop view - which is unreadable without zoom?