This question is a continuation of this question: (Oracle APEX – SQL – Creating a Sequential History and Calculating Days Between Each Phase)
Some background:
So for one of my applications I had decided that I needed the ability to capture more detailed metrics than I was previously doing. My group creates documents and specifically I wanted to know exactly how long (in days) that document spent in each phase of its development. The table for capturing this data is structured like so:
TBL_DOC_TIMELINE
DOC_ENTRY_ID DOC_ID DOC_STATUS DOC_CATEGORY DOC_DATE
1 123 Planned OPEN 06-05-2012
7 123 Draft OPEN 06-15-2012
38 123 Approval OPEN 06-20-2012
102 123 Published CLOSED 06-30-2012
All of our documents are using the same table for this function so I could not simply key on the DOC_ENTRY_ID though it could help. I needed to find the max DOC_ENTRY_ID for the DOC_ID and then calculate. I do this until I reach the DOC_CATEGORY of ‘CLOSED’ and which point ‘0’ is inserted in the cell as that is the end of that DOC_ID’s lifecycle. Like so:
DOC_ENTRY_ID DOC_ID DOC_STATUS DOC_CATEGORY DOC_DATE DOC_DURATION
1 123 Planned OPEN 06-05-2012 10
7 123 Draft OPEN 06-15-2012 5
38 123 Approval OPEN 06-20-2012 10
102 123 Published CLOSED 06-30-2012 0
This is all currently accomplished thanks to the brilliant Tony Andrews who provided the rough DRAFT for the View code that eventually became:
create or replace view DOC_TIMELINE as
select t.DOC_ENTRY_ID, t.DOC_ID, t.DOC_STATUS, t.DOC_CATEGORY, t.DOC_DATE
, case when DOC_CATEGORY = 'CLOSED' then 0
else lead(DOC_DATE) over (partition by DOC_ID order by DOC_ENTRY_ID)
- DOC_DATE
end as duration
from TBL_DOC_TIMELINE t;
What I now need to do:
This all works perfectly except that in my initial pass at this I forgot one very important part of my requirements. My goal is to know how long documents are spending in each phase but I completely neglected to realize that I need to gather this information in real time and not only after a document is CLOSED. With this current set up the View will look like this in the middle of its lifecycle:
DOC_ENTRY_ID DOC_ID DOC_STATUS DOC_CATEGORY DOC_DATE DOC_DURATION
1 123 Planned OPEN 06-05-2012 10
7 123 Draft OPEN 06-15-2012 5
38 123 Approval OPEN 06-20-2012 -
See the problem? If I need to know how long that document has spent in the Approval state then I need to wait until it has left that state for the duration to be calculated. So even though it might have been in that state for 20 days my metrics will not reflect that.
What I need to do is find some way to tweak the View code above to calculate this value against the SYSDATE() if the MAX(DOC_ENTRY_ID) for a given DOC_ID has a DOC_CATEGORY = ‘OPEN’.
assuming SYSDATE() = '06-29-2012'
DOC_ENTRY_ID DOC_ID DOC_STATUS DOC_CATEGORY DOC_DATE DOC_DURATION
1 123 Planned OPEN 06-05-2012 10
7 123 Draft OPEN 06-15-2012 5
38 123 Approval OPEN 06-20-2012 9
Does any of that make sense? I am imagining that I need to add another CASE to the View code but the sytanx is giving me some trouble. It is probably a simple solution to you guys but my familiarity with these kinds of cases and the sytanx allowed is limited and my research has not uncovered anything that relates.
I sincerely appreciate any and all help. Thanks guys!
Something like this?-
or since
lead(DOC_DATE)will always benullfor maxDOC_ENTRY_ID(for aDOC_ID)-