Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8383885
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T17:16:45+00:00 2026-06-09T17:16:45+00:00

This question is a continuation of this question: ( Oracle APEX – SQL –

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T17:16:46+00:00Added an answer on June 9, 2026 at 5:16 pm

    Something like this?-

    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
                  when lead(DOC_DATE) over (partition by DOC_ID order by DOC_ENTRY_ID)
                       - DOC_DATE is null then trunc(sysdate)-trunc(DOC_DATE)
                  else lead(DOC_DATE) over (partition by DOC_ID order by DOC_ENTRY_ID)
                       - DOC_DATE
                  end as duration
      from TBL_DOC_TIMELINE t;
    

    or since lead(DOC_DATE) will always be null for max DOC_ENTRY_ID (for a DOC_ID)-

    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
                  when lead(DOC_DATE) over (partition by DOC_ID order by DOC_ENTRY_ID) 
                          is null then trunc(sysdate)-trunc(DOC_DATE)
                  else lead(DOC_DATE) over (partition by DOC_ID order by DOC_ENTRY_ID)
                       - DOC_DATE
                  end as duration
      from TBL_DOC_TIMELINE t;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Continue Execution Only After .each() Completes This question is actually a continuation
A continuation from this question I need a SQL statement that returns the number
This question is a continuation of my previous question here zend models architecture (big
This question is in continuation to my previous question . I am trying to
This is a continuation of this question from yesterday . Here are my three
This is a continuation of this question: Original Question (SO) The answer to this
This is a continuation of the question posted in: How to load a jar
This is a continuation of the question here: JBoss - does app have to
This is a continuation of my previous question .NET regex engine returns no matches
Ok - this is in continuation from my earlier question about sending an email

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.