Reputation: 247
I currently have a CASE statement that checks to see whether certain tasks are completed or not, and then returns the date of the next task. Since the tasks are ordered, each WHEN statement becomes longer, checking each of the previous tasks to see if they're complete. For some reason, after the first WHEN statement, it's skipping straight to ELSE (it should be meeting the conditions of the second or third WHEN).
CASE
WHEN T.PRNAME = 'TECH PEP MEETING DATE' AND T.PRSTATUS != 2 THEN (SELECT TO_CHAR(MAX(T.PRFINISH),'DD/MM/YY') FROM PRTASK T WHERE T.PRPROJECTID = INV_INVESTMENTS.ID AND Upper(T.PRNAME) = 'TECH PEP MEETING DATE' AND PRISMILESTONE = 1)
WHEN (T.PRNAME = 'TECH PEP MEETING DATE' AND T.PRSTATUS = 2) AND (T.PRNAME = 'BRU MEETING DATE' AND T.PRSTATUS != 2) THEN (SELECT TO_CHAR(MAX(T.PRFINISH),'DD/MM/YY') FROM PRTASK T WHERE T.PRPROJECTID = INV_INVESTMENTS.ID AND Upper(T.PRNAME) = 'BRU MEETING DATE' AND PRISMILESTONE = 1)
WHEN (T.PRNAME = 'TECH PEP MEETING DATE' AND T.PRSTATUS = 2) AND (T.PRNAME = 'BRU MEETING DATE' AND T.PRSTATUS = 2) AND (T.PRNAME = 'TSC MEETING DATE' AND T.PRSTATUS != 2) THEN (SELECT TO_CHAR(MAX(T.PRFINISH),'DD/MM/YY') FROM PRTASK T WHERE T.PRPROJECTID = INV_INVESTMENTS.ID AND Upper(T.PRNAME) = 'TSC MEETING DATE' AND PRISMILESTONE = 1)
ELSE (SELECT TO_CHAR(MAX(T.PRFINISH),'DD/MM/YY') FROM PRTASK T WHERE T.PRPROJECTID = INV_INVESTMENTS.ID AND Upper(T.PRNAME) = 'END OF EVALUATE PHASE' AND PRISMILESTONE = 1)
END
Is there a specific way you need to address these types of multiple WHEN conditions?
EDIT: So after some feedback from you guys I agree with the idea that you only need to evaluate one task per WHEN because the CASE statement should exit once it finds its first TRUE statement. However, having updated it to:
CASE
WHEN UPPER(T.PRNAME) = 'EVALUATE TECH PEP MEETING DATE' AND T.PRSTATUS != 2 THEN (SELECT TO_CHAR(MAX(T.PRFINISH),'DD/MM/YY') FROM PRTASK T WHERE T.PRPROJECTID = INV_INVESTMENTS.ID AND Upper(T.PRNAME) = 'TECH PEP MEETING DATE' AND PRISMILESTONE = 1 AND ROWNUM = 1)
WHEN UPPER(T.PRNAME) = 'EVALUATE BRU MEETING DATE' AND T.PRSTATUS != 2 THEN (SELECT TO_CHAR(MAX(T.PRFINISH),'DD/MM/YY') FROM PRTASK T WHERE T.PRPROJECTID = INV_INVESTMENTS.ID AND Upper(T.PRNAME) = 'BRU MEETING DATE' AND PRISMILESTONE = 1 AND ROWNUM = 1)
WHEN UPPER(T.PRNAME) = 'EVALUATE TSC MEETING DATE' AND T.PRSTATUS != 2 THEN (SELECT TO_CHAR(MAX(T.PRFINISH),'DD/MM/YY') FROM PRTASK T WHERE T.PRPROJECTID = INV_INVESTMENTS.ID AND Upper(T.PRNAME) = 'TSC MEETING DATE' AND PRISMILESTONE = 1 AND ROWNUM = 1)
ELSE (SELECT TO_CHAR(MAX(T.PRFINISH),'DD/MM/YY') FROM PRTASK T WHERE T.PRPROJECTID = INV_INVESTMENTS.ID AND Upper(T.PRNAME) = 'END OF EVALUATE PHASE' AND PRISMILESTONE = 1)
END
I am now getting:
ORA-01427: single-row subquery returns more than one row
Not sure why this is the case, especially having put ROWNUM = 1
on the end to ensure only one result is returned.
When running the THEN by itself:
SELECT TO_CHAR(MAX(T.PRFINISH),'DD/MM/YY')
FROM PRTASK T
WHERE T.PRPROJECTID = INV_INVESTMENTS.ID
AND Upper(T.PRNAME) = 'TECH PEP MEETING DATE'
AND PRISMILESTONE = 1
AND ROWNUM = 1
I'm getting one result. If I'm right in thinking that the CASE statement will exit once it finds its first TRUE statement, why is this finding multiple rows?
EDIT 2: Ok - so I've been playing around with this some more (because I still cannot find a logical answer and I've made some headway. I have now changed the way the query is structured to the following:
SELECT DISTINCT To_Char(T.PRFINISH, 'DD/MM/YY'),
T.PRNAME
FROM PRTASK T
LEFT OUTER JOIN INV_INVESTMENTS ON T.PRPROJECTID = INV_INVESTMENTS.ID
WHERE T.PRNAME = CASE
WHEN (T.PRNAME = 'Concept Tech PEP Meeting Date' AND T.PRSTATUS != 2) THEN 'Concept Tech PEP Meeting Date'
WHEN (T.PRNAME = 'Concept BRU Meeting Date' AND T.PRSTATUS != 2) THEN 'Concept BRU Meeting Date'
WHEN (T.PRNAME = 'End of Concept Phase' AND T.PRSTATUS != 2) THEN 'End of Concept Phase'
WHEN (T.PRNAME = 'Evaluate Tech PEP Meeting Date' AND T.PRSTATUS != 2) THEN 'Evaluate Tech PEP Meeting Date'
WHEN (T.PRNAME = 'Evaluate BRU Meeting Date' AND T.PRSTATUS != 2) THEN 'Evaluate BRU Meeting Date'
WHEN (T.PRNAME = 'Evaluate TSC Meeting Date' AND T.PRSTATUS != 2) THEN 'Evaluate TSC Meeting Date'
WHEN (T.PRNAME = 'End of Evaluate Phase' AND T.PRSTATUS != 2) THEN 'End of Evaluate Phase'
WHEN (T.PRNAME = 'End of Analyse Phase' AND T.PRSTATUS != 2) THEN 'End of Analyse Phase'
WHEN (T.PRNAME = 'End of Design Phase' AND T.PRSTATUS != 2) THEN 'End of Design Phase'
WHEN (T.PRNAME = 'End of Build Phase' AND T.PRSTATUS != 2) THEN 'End of Build Phase'
WHEN (T.PRNAME = 'End of Test Phase' AND T.PRSTATUS != 2) THEN 'End of Test Phase'
WHEN (T.PRNAME = 'In Service' AND T.PRSTATUS != 2) THEN 'In Service'
WHEN (T.PRNAME = 'End of Implement Phase' AND T.PRSTATUS != 2) THEN 'End of Implement Phase'
WHEN (T.PRNAME = 'End of Closure Phase' AND T.PRSTATUS != 2) THEN 'End of Closure Phase'
ELSE 'In Service'
END
AND INV_INVESTMENTS.CODE = '007058'
Now, however, I'm getting multiple WHEN statements returning values. Can anyone confirm whether or not CASE statements truly only return the first TRUE value?
Upvotes: 5
Views: 31863
Reputation: 1455
uhm,
i tried to sqlfiddle your question, but there is to much unknown tables used. To get better help, try to build some sqlfiddle your helpers can use.
After some research it seems to me, that oracle does not in fact checks, if your query returns only one row. it checks, if your syntax seems to allow only one row. I'm not sure about this.
Try using a max(x) around all your singular subquery-return-fields and hae a look, if oracle is accepting this. If you really get only one row, a max() does not change your value, so use it.
Upvotes: 2
Reputation: 1746
Your CASE WHEN
condition conflicts to each other, you have problem in using AND
. Your 2nd and 3rd WHEN
will always return FALSE
, why ? look at this line..
WHEN (T.PRNAME = 'TECH PEP MEETING DATE' AND T.PRSTATUS = 2)
AND (T.PRNAME = 'BRU MEETING DATE' AND T.PRSTATUS != 2)
THEN...
This one T.PRNAME = 'TECH PEP MEETING DATE
is conflicted to the AND
condition next to it AND (T.PRNAME = 'BRU MEETING DATE'
that condition will never return TRUE
same on your 3rd WHEN
Using AND
will only return true, if the two condition between AND
is true.
By reading your codes, it seems your AND
in each WHEN
of your code needs to replace by OR
to correct/remove the conflicting condition in your column T.PRNAME
Below code are yours, i modified it replacing the AND
by OR
to correct your condition.
CASE
WHEN T.PRNAME = 'TECH PEP MEETING DATE' AND T.PRSTATUS != 2
THEN (SELECT TO_CHAR(MAX(T.PRFINISH),'DD/MM/YY') FROM PRTASK T WHERE T.PRPROJECTID = INV_INVESTMENTS.ID AND Upper(T.PRNAME) = 'TECH PEP MEETING DATE' AND PRISMILESTONE = 1)
WHEN (T.PRNAME = 'TECH PEP MEETING DATE' AND T.PRSTATUS = 2) OR (T.PRNAME = 'BRU MEETING DATE' AND T.PRSTATUS != 2)
THEN (SELECT TO_CHAR(MAX(T.PRFINISH),'DD/MM/YY') FROM PRTASK T WHERE T.PRPROJECTID = INV_INVESTMENTS.ID AND Upper(T.PRNAME) = 'BRU MEETING DATE' AND PRISMILESTONE = 1)
WHEN (T.PRNAME = 'TECH PEP MEETING DATE' AND T.PRSTATUS = 2) OR (T.PRNAME = 'BRU MEETING DATE' AND T.PRSTATUS = 2) AND (T.PRNAME = 'TSC MEETING DATE' AND T.PRSTATUS != 2)
THEN (SELECT TO_CHAR(MAX(T.PRFINISH),'DD/MM/YY') FROM PRTASK T WHERE T.PRPROJECTID = INV_INVESTMENTS.ID AND Upper(T.PRNAME) = 'TSC MEETING DATE' AND PRISMILESTONE = 1)
ELSE (SELECT TO_CHAR(MAX(T.PRFINISH),'DD/MM/YY') FROM PRTASK T WHERE T.PRPROJECTID = INV_INVESTMENTS.ID AND Upper(T.PRNAME) = 'END OF EVALUATE PHASE' AND PRISMILESTONE = 1)
END
Hope this helps.
Upvotes: 1
Reputation: 511
Trying to answer the ROWNUM=1 question, are you sure you are running in one of the THEN branches? You do not have ROWNUM=1 specified in the ELSE branch. Does it help to also add ROWNUM=1 there?
Upvotes: 2
Reputation: 5733
First, fix your case statement for what lad2025 points out, but not using any OR
, and simplified as
CASE
WHEN T.PRNAME = 'TECH PEP MEETING DATE' AND T.PRSTATUS != 2 THEN ...
WHEN T.PRNAME = 'BRU MEETING DATE' AND T.PRSTATUS != 2 THEN ...
WHEN T.PRNAME = 'TSC MEETING DATE' AND T.PRSTATUS != 2 THEN ...
ELSE ...
END
Then further simplify using the case to use the same query
(
SELECT TO_CHAR(MAX(T.PRFINISH),'DD/MM/YY')
FROM PRTASK T
WHERE T.PRPROJECTID = INV_INVESTMENTS.ID
AND PRISMILESTONE = 1
AND Upper(T.PRNAME) =
CASE
WHEN T.PRNAME = 'TECH PEP MEETING DATE' AND T.PRSTATUS != 2 THEN 'TECH PEP MEETING DATE'
WHEN T.PRNAME = 'BRU MEETING DATE' AND T.PRSTATUS != 2 THEN 'BRU MEETING DATE'
WHEN T.PRNAME = 'TSC MEETING DATE' AND T.PRSTATUS != 2 THEN 'TSC MEETING DATE'
ELSE 'END OF EVALUATE PHASE'
END
)
Upvotes: 2
Reputation: 894
The T.PRNAME = 'TECH PEP MEETING DATE' AND T.PRNAME = 'BRU MEETING DATE'? I think we're having an overlapping conditions here.
My high assumption here is that you have 1 table with many task with status beside, then I think what should happen here is something as below.
First is your table of tasks, I created something my own.
CREATE TABLE #testtask
(
PRID INT
, PRNAME varchar(50)
, PRSTATUS INT
, PREREQ INT
)
INSERT INTO #testtask VALUES
(1,'TECH PEP MEETING DATE',1,0),
(2,'BRU MEETING DATE',1,1),
(3,'TSC MEETING DATE',1,2)
Must might be something like this
Then, I created a left join on its own table related to its pre-requisite task.
SELECT
t1.PRNAME AS [Job]
, t1.PRSTATUS AS [JobStatus]
, t2.PRNAME AS [PreReqJob]
, t2.PRSTATUS AS [PreReqStatus]
INTO #taskList
FROM #testtask t1
LEFT JOIN #testtask t2
ON
t1.PREREQ = t2.PRID
and with this following result.
before getting into what I believe is your script checking for each task state with pre-requisite tasks.
SELECT
CASE
WHEN tl.[Job] = 'TECH PEP MEETING DATE' AND tl.[JobStatus] != 2
THEN
-- do your max select here for 'Tech pep'
WHEN tl.[Job] = 'BRU MEETING DATE' AND tl.[JobStatus] != 2 AND tl.[PreReqStatus] = 2
THEN
-- do your max select here for 'Bru meet'
WHEN tl.[Job] = 'TSC MEETING DATE' AND tl.[JobStatus] != 2 AND tl.[PreReqStatus] = 2
THEN
-- do your max select here for 'Tsc meet'
ELSE
-- do your default max date
END AS [Date]
FROM #taskList AS tl
Please get the concept alone as I do not have your actual tables. You would pretty much have an error if you copy the whole thing. Hopefully this helps :)
Upvotes: 4