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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T22:38:12+00:00 2026-05-14T22:38:12+00:00

I have the following Oracle query: SELECT id, DECODE(state, ‘Open’, state_in, NULL) AS open_in,

  • 0

I have the following Oracle query:

SELECT id,
       DECODE(state, 'Open', state_in, NULL) AS open_in,
       DECODE(state, 'Not Open', state_in, NULL) AS open_out,
FROM (
       SELECT id,
              CASE WHEN state = 'Open'
                   THEN 'Open'
                   ELSE 'Not Open'
              END AS state,
              TRUNC(state_time) AS state_in
       FROM ...
     )

This gives me data like the following:

id  open_in              open_out
1   2009-03-02 00:00:00
1                        2009-03-05 00:00:00
1   2009-03-11 00:00:00
1                        2009-03-26 00:00:00
1                        2009-03-24 00:00:00
1                        2009-04-13 00:00:00

What I would like is data like this:

id  open_in              open_out
1   2009-03-02 00:00:00  2009-03-05 00:00:00
1   2009-03-11 00:00:00  2009-03-24 00:00:00

That is, keep all the unique pairs of id/open_in and pair with them the earliest open_out that follows open_in. There can be any number of unique open_in values for a given id, and any number of unique open_out values. It is possible that a unique id/open_in will not have a matching open_out value, in which case open_out should be null for that row.

I feel like some analytic function, maybe LAG or LEAD, would be useful here. Perhaps I need MIN used with a PARTITION.

  • 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-14T22:38:13+00:00Added an answer on May 14, 2026 at 10:38 pm

    It can be done a little bit simpler. First let’s create a sample table:

    SQL> create table mytable (id,state,state_time)
      2  as
      3  select 1, 'Open', date '2009-03-02' from dual union all
      4  select 1, 'Closed', date '2009-03-05' from dual union all
      5  select 1, 'Open', date '2009-03-11' from dual union all
      6  select 1, 'Shut down', date '2009-03-26' from dual union all
      7  select 1, 'Wiped out', date '2009-03-24' from dual union all
      8  select 1, 'Demolished', date '2009-04-13' from dual
      9  /
    
    Table created.
    

    The data equals the output of your select statement:

    SQL> SELECT id,
      2         DECODE(state, 'Open', state_in, NULL) AS open_in,
      3         DECODE(state, 'Not Open', state_in, NULL) AS open_out
      4  FROM (
      5         SELECT id,
      6                CASE WHEN state = 'Open'
      7                     THEN 'Open'
      8                     ELSE 'Not Open'
      9                END AS state,
     10                TRUNC(state_time) AS state_in
     11         FROM mytable
     12       )
     13  /
    
            ID OPEN_IN             OPEN_OUT
    ---------- ------------------- -------------------
             1 02-03-2009 00:00:00
             1                     05-03-2009 00:00:00
             1 11-03-2009 00:00:00
             1                     26-03-2009 00:00:00
             1                     24-03-2009 00:00:00
             1                     13-04-2009 00:00:00
    
    6 rows selected.
    

    And here is the slightly easier query:

    SQL> select id
      2       , min(case when state = 'Open' then state_time end)  open_in
      3       , min(case when state != 'Open' then state_time end) open_out
      4    from ( select id
      5                , state
      6                , state_time
      7                , max(x) over (partition by id order by state_time) grp
      8             from ( select id
      9                         , state
     10                         , state_time
     11                         , case state
     12                           when 'Open' then
     13                             row_number() over (partition by id order by state_time)
     14                           end x
     15                      from mytable
     16                  )
     17         )
     18   group by id
     19       , grp
     20   order by id
     21       , open_in
     22  /
    
            ID OPEN_IN             OPEN_OUT
    ---------- ------------------- -------------------
             1 02-03-2009 00:00:00 05-03-2009 00:00:00
             1 11-03-2009 00:00:00 24-03-2009 00:00:00
    
    2 rows selected.
    

    Regards,
    Rob.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a following oracle query: SELECT a.USER_ID, c.first_name, c.last_name, TO_CHAR( b.logon_date, 'MM/DD/YYYY HH:MI:SS
I have following statements in Oracle query window in VS2010, It does not show
I have the following Oracle query SELECT * FROM table WHERE date_opened BETWEEN ((TO_DATE('2011-08-01',
I have the following oracle query which runs fine SELECT c.customercode, s.sales_id FROM customers
I have the following query that runs in my Oracle database and I want
I have written the following query using the documentation at: Oracle Documentation to copy
Using Oracle 11g release 2, the following query gives an ORA-01790: expression must have
I have the following query: SELECT wm_concat(DISTINCT NAME) as Methods FROM TPM_TRAININGPLAN JOIN TPM_DELIVERYMETHODS
I have the following query SELECT * FROM( SELECT CASE WHEN TO_CHAR (ADD_MONTHS(:DATEINPUT, 1),
I have the following SQL query: select ID, COLUMN1, COLUMN2 from (select ID, COLUMN1,

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.