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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T22:01:55+00:00 2026-06-15T22:01:55+00:00

does anyone have suggestions for this problem? i am trying to use oracle SQL

  • 0

does anyone have suggestions for this problem? i am trying to use oracle SQL to consolidate spans of effective_dt to expiration_dt where values in col_a, col_b, and col_c remain constant, but only for consecutive records where there is no change in any of the 3 columns in between.

if it helps, it is safe to assume that the effective date of the next record (by employee) is equal to the previous record plus 1 day.

i tried with min(), max() and group by, but the problem is the scenario below will return 12/1-12/31. then i tried with lead() function, but the problem is i do not know in advance how many records i will need to consolidate.

assume that i can get the data into the form below:

+----------+--------------+---------------+---------+---------+---------+
| employee | effective_dt | expiration_dt |  col_a  |  col_b  |  col_c  |
+----------+--------------+---------------+---------+---------+---------+
|     0001 | 12/1/2012    | 12/4/2012     | value_a | value_a | value_a |
|     0001 | 12/5/2012    | 12/6/2012     | value_a | value_a | value_a |
|     0001 | 12/7/2012    | 12/10/2012    | value_a | value_a | value_a |
|     0001 | 12/11/2012   | 12/17/2012    | value_a | value_b | value_a |
|     0001 | 12/18/2012   | 12/31/2012    | value_a | value_a | value_a |
+----------+--------------+---------------+---------+---------+---------+    

expected result:

+----------+--------------+---------------+---------+---------+---------+
| employee | effective_dt | expiration_dt |  col_a  |  col_b  |  col_c  |
+----------+--------------+---------------+---------+---------+---------+
|     0001 | 12/1/2012    | 12/10/2012    | value_a | value_a | value_a |
|     0001 | 12/11/2012   | 12/17/2012    | value_a | value_b | value_a |
|     0001 | 12/18/2012   | 12/31/2012    | value_a | value_a | value_a |
+----------+--------------+---------------+---------+---------+---------+

attempt 1:

SELECT employee,
  MIN(effective_dt),
  MAX(expiration_dt),
  col_a,
  col_b,
  col_c
FROM
  (SELECT employee, effective_dt, ... FROM table_x, table_y, ... where...
  ) table_a
GROUP BY employee,
  col_a,
  col_b,
  col_c;

attempt 2:

SELECT employee,
  effective_dt,
  lead(expiration_dt, 1) over (partition BY employee, col_a, col_b, col_c order by effective_dt) expiration_dt,
  col_a,
  col_b,
  col_c
FROM
  (SELECT employee, effective_dt, ... FROM table_x, table_y, ... where...
  ) table_a;

thank you!

  • 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-15T22:01:56+00:00Added an answer on June 15, 2026 at 10:01 pm

    In case we can, as you state, safely assume that next record equals previous record + 1 day, then we can chain these records up:

    SQL Fiddle

    Oracle 11g R2 Schema Setup:

    CREATE TABLE t
        (employee int, effective_dt timestamp, expiration_dt timestamp, col_a varchar2(7), col_b varchar2(7), col_c varchar2(7))
    ;
    
    INSERT ALL 
        INTO t (employee, effective_dt, expiration_dt, col_a, col_b, col_c)
             VALUES (0001, '01-Dec-2012 12:00:00 AM', '04-Dec-2012 12:00:00 AM', 'value_a', 'value_a', 'value_a')
        INTO t (employee, effective_dt, expiration_dt, col_a, col_b, col_c)
             VALUES (0001, '05-Dec-2012 12:00:00 AM', '06-Dec-2012 12:00:00 AM', 'value_a', 'value_a', 'value_a')
        INTO t (employee, effective_dt, expiration_dt, col_a, col_b, col_c)
             VALUES (0001, '07-Dec-2012 12:00:00 AM', '10-Dec-2012 12:00:00 AM', 'value_a', 'value_a', 'value_a')
        INTO t (employee, effective_dt, expiration_dt, col_a, col_b, col_c)
             VALUES (0001, '11-Dec-2012 12:00:00 AM', '17-Dec-2012 12:00:00 AM', 'value_a', 'value_b', 'value_a')
        INTO t (employee, effective_dt, expiration_dt, col_a, col_b, col_c)
             VALUES (0001, '18-Dec-2012 12:00:00 AM', '19-Dec-2012 12:00:00 AM', 'value_a', 'value_b', 'value_a')
        INTO t (employee, effective_dt, expiration_dt, col_a, col_b, col_c)
             VALUES (0001, '20-Dec-2012 12:00:00 AM', '31-Dec-2012 12:00:00 AM', 'value_a', 'value_a', 'value_a')
    SELECT * FROM dual
    ;
    

    Query 1:

    select employee, min(effective_dt), max(expiration_dt), col_a, col_b, col_c 
      from ( select t.*, 
                    case when
                         col_a = lag(col_a) over (partition by employee order by expiration_dt asc)    
                     and col_b = lag(col_b) over (partition by employee order by expiration_dt asc) 
                     and col_c = lag(col_c) over (partition by employee order by expiration_dt asc) 
                    then 0 else 1 end start_of_chain 
               from t
      )
     connect by effective_dt = prior expiration_dt + 1  and start_of_chain = 0  
       start with start_of_chain = 1
       group by connect_by_root(effective_dt), employee, col_a, col_b, col_c
    order by 2 
    

    Results:

    | EMPLOYEE |               MIN(EFFECTIVE_DT) |              MAX(EXPIRATION_DT) |   COL_A |   COL_B |   COL_C |
    --------------------------------------------------------------------------------------------------------------
    |        1 | December, 01 2012 00:00:00+0000 | December, 10 2012 00:00:00+0000 | value_a | value_a | value_a |
    |        1 | December, 11 2012 00:00:00+0000 | December, 19 2012 00:00:00+0000 | value_a | value_b | value_a |
    |        1 | December, 20 2012 00:00:00+0000 | December, 31 2012 00:00:00+0000 | value_a | value_a | value_a |
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone have any suggestions on how to edit an <a href=''> link in
Does anyone have a suggestions for a superb cool image gallery that I can
Does anyone have any suggestions about where I can find a C# implementation for
Does anyone else get this problem or know a solution / workaround I could
Does anyone have a translate function for x/y positions after rotation in javascript? for
Does anyone have recommendations on the best Online Store for non-technical users of Joomla?
Does anyone have an idea on where is it best to upload a PHP
Does anyone have any examples on how to integrate a task scheduler in Bottle.
Does anyone have an idea of how to capture logs continuously and update a
does anyone have a nice solution to get de price wich is a variable

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.