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 6138099
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:48:29+00:00 2026-05-23T17:48:29+00:00

I’m trying to determine the total elapsed time to complete a set of processes

  • 0

I’m trying to determine the total elapsed time to complete a set of processes for a multithreaded application which keeps track of start and end times in a table. The easiest way to describe my problem is with an example.

Here’s a dumbed-down version of the table (we’ll call it processes) I’m working with:

| id  | start_date          | end_date            |
---------------------------------------------------
|  1  | 07/15/2011 12:00:00 | 07/15/2011 12:01:00 |
|  2  | 07/15/2011 12:00:00 | 07/15/2011 12:02:00 |
|  3  | 07/15/2011 12:00:00 | 07/15/2011 12:03:00 |
|  4  | 07/15/2011 12:01:00 | 07/15/2011 12:05:00 |
|  5  | 07/15/2011 12:01:00 | 07/15/2011 12:03:00 |
|  6  | 07/15/2011 12:03:00 | 07/15/2011 12:04:00 |
|  7  | 07/15/2011 12:03:00 | 07/15/2011 12:07:00 |
|  8  | 07/15/2011 12:03:00 | 07/15/2011 12:06:00 |
|  9  | 07/15/2011 12:04:00 | 07/15/2011 12:05:00 |
| 10  | 07/15/2011 12:05:00 | 07/15/2011 12:07:00 |
| 11  | 07/15/2011 12:08:00 | 07/15/2011 12:09:00 |

With such a small sample of data it’s easy enough to visualize this (I’m assuming a thread can finish with a process and instantaneously pick up the next one with no overhead for the purposes of this question)

12:XX: | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
Thread1: 1---4---------------10------]   11--]
Thread2: 2-------]   6---9---]
Thread3: 3-----------7---------------]
Thread4:     5-------8-----------]

And from there you can easily tell that the total time spent working on the 11 processes was 8 minutes.

The problem arises because I am dealing with thousands of records, and there are some periods of time where no processing is happening at all.

How can I get this result using a PL/SQL query selecting from the table?

  • 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-05-23T17:48:30+00:00Added an answer on May 23, 2026 at 5:48 pm

    Unless I’m missing something, all you need is the difference between the lowest start date and the highest end date:

    SELECT (MAX(end_date) - MIN(start_time))*24*60 AS elapsed_time
    FROM   processes;
    

    This’ll return the time elapsed in minutes.


    Based on the comment, it seems that you want the sum of the time elapsed for each process? If that’s the case, it’s just a smallish variation on the earlier answer:

    SELECT sum(end_date - start_time)*24*60 AS elapsed_time
    FROM   processes;
    

    Obviously this answer doesn’t deal with identify which processes to get the total for, but that’s not addressed in the question at all, so I’m assuming that’s not an issue.


    I see now that this doesn’t provide the answer that you’re looking for either, because it counts time worked by multiple processes multiple times. I believe the following will work. This is based on @Rajesh’s sample data from the other answer.

    SELECT SUM (new_end - new_start) * 24 * 60 * 60
    FROM   (SELECT   DISTINCT
                     MIN (LEAST (a.start_time, b.start_time)) AS new_start, 
                     MAX (GREATEST (a.end_time, b.end_time)) AS new_end
            FROM     (SELECT   ROWNUM AS rnum, t1.* FROM t1) a
                     INNER JOIN t1 b
                     ON a.start_time <= b.end_time AND a.end_time >= b.start_time
            GROUP BY rnum)
    

    Basically, we’re joining each to every other row that overlaps with it and taking the earliest start time and latest end time from each of those pairings. Once we have that, we can use the distinct set to remove the duplicates, which will give us the distinct time periods.

    I believe there is still one flaw here, which is cascading time periods: period A overlaps with period B and period B overlaps with period C, but period C does not overlap with period A. I think this issue is solvable, I just haven’t quite figured it out yet.


    Okay, one more time: rather than joining once between the table and itself, this version uses a recursive join to follow all of the cascades to their end point.

    SELECT SUM (new_end - new_start) * 24 * 60 * 60
    FROM   (SELECT   DISTINCT MIN (start_time) AS new_start, 
                              MAX (end_time) AS new_end
            FROM     (SELECT  start_time, 
                              end_time, 
                              CONNECT_BY_ROOT rnum AS rnum
                      FROM    (SELECT     ROWNUM AS rnum, t1.* FROM t1) a
                      CONNECT BY NOCYCLE 
                             a.start_time <= PRIOR end_time 
                             AND a.end_time >= PRIOR start_time)
            GROUP BY rnum);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I'm trying to select an H1 element which is the second-child in its group
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Specifically, suppose I start with the string string =hello \'i am \' me And
I am trying to render a haml file in a javascript response like so:
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.