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

  • Home
  • SEARCH
  • 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 515637
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T07:38:55+00:00 2026-05-13T07:38:55+00:00

I have a table in Oracle 10 that is defined like this: LOCATION HOUR

  • 0

I have a table in Oracle 10 that is defined like this:

LOCATION   HOUR              STATUS
--------------------------------------
10         12/10/09 5:00PM      1
10         12/10/09 6:00PM      1
10         12/10/09 7:00PM      2
10         12/10/09 8:00PM      1
10         12/10/09 9:00PM      3
10         12/10/09 10:00PM     3
10         12/10/09 11:00PM     3

This table continues for various locations and for a small number of status values. Each row covers one hour for one location. Data is collected from a particular location over the course of that hour, and processed in chunks. Sometimes the data is available, sometimes it isn’t, and that information is encoded in the status. I am trying to find runs of a particular status, so that I could convert the above table into something like:

LOCATION   STATUS     START               END
-----------------------------------------------------------
10         1          12/10/09 5:00PM     12/10/09 7:00PM
10         2          12/10/09 7:00PM     12/10/09 8:00PM  
10         1          12/10/09 8:00PM     12/10/09 9:00PM
10         3          12/10/09 9:00PM     12/11/09 12:00AM 

Basically condensing the table into rows that define each stretch of a particular status. I have tried various tricks, like using lead/lag to figure out where starts and ends are and such, but all of them have met with failure. The only trick that works so far is going one by one through the values programatically, which is slow. Any ideas for doing it directly in Oracle? Thanks!

  • 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-13T07:38:55+00:00Added an answer on May 13, 2026 at 7:38 am

    Here’s an ANSI SQL solution:

    select      t1.location
    ,           t1.status
    ,           min(t1.hour)                                      AS "start" -- first of stretch of same status
    ,           coalesce(t2.hour, max(t1.hour) + INTERVAL '1' HOUR) AS "end"
    from        t_intervals t1             -- base table, this is what we are condensing
    left join   t_intervals t2             -- finding the first datetime after a stretch of t1
    on          t1.location = t2.location  -- demand same location
    and         t1.hour     < t2.hour      -- demand t1 before t2
    and         t1.status  != t2.status    -- demand different status 
    left join   t_intervals t3             -- finding rows not like t1, with hour between t1 and t2
    on          t1.location = t3.location
    and         t1.status  != t3.status
    and         t1.hour     < t3.hour
    and         t2.hour     > t3.hour
    where       t3.status is null          -- demand that t3 does not exist, in other words, t2 marks a status transition
    group by    t1.location                -- condense on location, status.
    ,           t1.status
    ,           t2.hour                    -- this pins the status transition
    order by    t1.location
    ,           t1.status
    ,           min(t1.hour)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this table in an Oracle DB which has a primary key defined
I have a table like this (Oracle, 10) Account Bookdate Amount 1 20080101 100
I have an entity that maps to an external oracle table which is one
Say I have an Oracle PL/SQL block that inserts a record into a table
We've got a view that's defined like this CREATE VIEW aView as SELECT *
I have a column in a non-partitioned Oracle table defined as VARCHAR2(50); the column
I have an Oracle table which contains event log messages for an application. We
I'll simplify the problem as much as possible: I have an oracle table: row_priority,
I have an Access 2002 application which links an Oracle table via ODBC with
I have a table, users, in an Oracle 9.2.0.6 database. Two of the fields

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.