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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T03:33:55+00:00 2026-05-22T03:33:55+00:00

I have a table like the following, each entry a change in STATUS on

  • 0

I have a table like the following, each entry a change in STATUS on the given time.
The status can be repeated because other columns have sub-status information.

How can I get a percentage time for each status by, say, hour?

NAME STATUS_CHANGE_TIME  STATUS
foo  15-MAY-11 18:52     A
foo  15-MAY-11 18:38     A
foo  15-MAY-11 18:33     B
foo  15-MAY-11 16:53     A
foo  15-MAY-11 16:47     B
foo  15-MAY-11 13:37     A
foo  15-MAY-11 13:33     C
foo  15-MAY-11 10:23     C
foo  15-MAY-11 10:17     A
foo  ...

Desired return:

HH24  STATUS  PERCENT    
10  ...
11    C       100        (No entries; last change was to C)
12    C       100        ""                       ""
13    C        62
13    A        38        (From C to A at :37 with 23 mins left; 23/60 ~ 38%)
14    A       100
15    A       100
16    A        90        (= A for first 47 minutes, then for another 7)
16    B        10        (16:53 - 16:47 = 6 minutes or 10% of an hour)
17    A       100
18 ... etc.
  • 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-22T03:33:56+00:00Added an answer on May 22, 2026 at 3:33 am

    Great question, this was an interesting challenge!

    What you need is an ancillary table to store each time division (in this case, hours), then join to it where the status updates overlap. LEAD() can grab the next status entry to check when it was, and GREATEST() and LEAST() can figure out which time is applicable for the start/end of the status for each hour.

    Of course, this is much easier explained in an example. Here is the HOURS table needed:

    SQL> CREATE TABLE hours (HOUR NUMBER(2), start_m date, end_m date);
    
    Table created.
    
    SQL> BEGIN
      2      FOR i IN 0..23 LOOP
      3          INSERT INTO hours VALUES(i, to_date(lpad(i, 2, '0')||':00:00', 'HH24:MI:SS')
      4                                    , to_date(lpad(i, 2, '0')||':59:59', 'HH24:MI:SS'));
      5      END loop;
      6      COMMIT;
      7  END;
      8  /
    
    PL/SQL procedure successfully completed.
    

    The following is just population of your test data from your question.

    SQL> CREATE TABLE status_updates (NAME VARCHAR2(3), status_change_time DATE, status CHAR(1));
    
    Table created.
    
    SQL> INSERT INTO status_updates VALUES ('foo',TO_DATE('15-MAY-11 18:52', 'DD-MON-RR HH24:MI:SS'), 'A');
    
    1 row created.
    
    SQL> INSERT INTO status_updates VALUES ('foo',TO_DATE('15-MAY-11 18:38', 'DD-MON-RR HH24:MI:SS'), 'A');
    
    1 row created.
    
    SQL> INSERT INTO status_updates VALUES ('foo',TO_DATE('15-MAY-11 18:33', 'DD-MON-RR HH24:MI:SS'), 'B');
    
    1 row created.
    
    SQL> INSERT INTO status_updates VALUES ('foo',TO_DATE('15-MAY-11 16:53', 'DD-MON-RR HH24:MI:SS'), 'A');
    
    1 row created.
    
    SQL> INSERT INTO status_updates VALUES ('foo',TO_DATE('15-MAY-11 16:47', 'DD-MON-RR HH24:MI:SS'), 'B');
    
    1 row created.
    
    SQL> INSERT INTO status_updates VALUES ('foo',TO_DATE('15-MAY-11 13:37', 'DD-MON-RR HH24:MI:SS'), 'A');
    
    1 row created.
    
    SQL> INSERT INTO status_updates VALUES ('foo',TO_DATE('15-MAY-11 13:33', 'DD-MON-RR HH24:MI:SS'), 'C');
    
    1 row created.
    
    SQL> INSERT INTO status_updates VALUES ('foo',TO_DATE('15-MAY-11 10:23', 'DD-MON-RR HH24:MI:SS'), 'C');
    
    1 row created.
    
    SQL> INSERT INTO status_updates VALUES ('foo',TO_DATE('15-MAY-11 10:17', 'DD-MON-RR HH24:MI:SS'), 'A');
    
    1 row created.
    
    SQL> commit;
    
    Commit complete.
    

    Now here is the select statement to get the required percentages.

    SELECT t.NAME, t.HOUR, t.status, sum(round((status_end_h-start_status_h)*24*100)) per_cent
    FROM   (
        SELECT A.NAME
        ,      A.status
        ,      A.status_change_time
        ,      A.next_change_time
        ,      b.HOUR
        ,      greatest(status_change_time, trunc(status_change_time)+(b.start_m-trunc(b.start_m))) start_status_h
        ,      least(next_change_time, trunc(next_change_time)+(b.end_m-trunc(b.end_m))) status_end_h
        FROM   (
            SELECT NAME
            ,      status
            ,      status_change_time
            ,      lead(status_change_time) OVER (ORDER BY NAME, status_change_time) next_change_time
            FROM   status_updates
        ) A, hours b
        WHERE  TO_CHAR(b.start_m, 'HH24:MI:SS') BETWEEN TO_CHAR(A.status_change_time, 'HH24:MI:SS') AND TO_CHAR(A.next_change_time, 'HH24:MI:SS')
        OR     TO_CHAR(b.end_m, 'HH24:MI:SS') BETWEEN TO_CHAR(A.status_change_time, 'HH24:MI:SS') AND TO_CHAR(A.next_change_time, 'HH24:MI:SS')
        OR    (TO_CHAR(A.status_change_time, 'HH24:MI:SS') BETWEEN TO_CHAR(b.start_m, 'HH24:MI:SS') AND TO_CHAR(b.end_m, 'HH24:MI:SS')
        AND    TO_CHAR(A.next_change_time, 'HH24:MI:SS') BETWEEN TO_CHAR(b.start_m, 'HH24:MI:SS') AND TO_CHAR(b.end_m, 'HH24:MI:SS'))
    ) t
    GROUP BY t.NAME, t.HOUR, t.status
    ORDER BY t.HOUR;
    
    NAM       HOUR S   PER_CENT                                                     
    --- ---------- - ----------                                                     
    foo         10 A         10                                                     
    foo         10 C         62                                                     
    foo         11 C        100                                                     
    foo         12 C        100                                                     
    foo         13 A         38                                                     
    foo         13 C         62                                                     
    foo         14 A        100                                                     
    foo         15 A        100                                                     
    foo         16 A         90                                                     
    foo         16 B         10                                                     
    foo         17 A        100                                                     
    
    NAM       HOUR S   PER_CENT                                                     
    --- ---------- - ----------                                                     
    foo         18 A         78                                                     
    foo         18 B          8                                                     
    
    13 rows selected.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a table in sqlite database like following. id status ============= x 0
I have a table like the following: ID Output ------------------------- 01ABC1 AB 01ABC2 AB
Say I have a database table like the following: FileID | FileName | FileSize
I have a table that looks like the following: <table class=theClass> <tr> <td class=anotherClass><strong>Label1:</strong></td>
I have a table with a structure like the following: LocationID AccountNumber long-guid-here 12345
I have a table in MySQL That looks like the following: date |storenum |views
I have a string like following: CREATE GLOBAL TEMPORARY TABLE some_temp_table_name or CREATE GLOBAL
I have a scenario outline table that looks like the following: Scenario Outline: Verify
I have the following table and would like to replace the string text of
I have a sample file like the following: CREATE GLOBAL TEMPORARY TABLE tt_temp_user_11 (

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.