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

The Archive Base Latest Questions

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

I have different records with columns Start Date and End Date. If I sort

  • 0

I have different records with columns Start Date and End Date.
If I sort records in ascending order of start date.
and want to find difference between start date of one row and end date of previous row consecutively.

e.g.
Table: Data
------------
Date1           Date2           
13-DEC-2011     15-DEC-2011      
18-DEC-2011     16-DEC-2011     
21-DEC-2011     24-DEC-2011  

one more query if I have a third column
say ID and I want difference grouped by these ID’s
e.g

              ID          Date1             Date2           
              1     13-DEC-2011     15-DEC-2011      
              1     18-DEC-2011     16-DEC-2011     
              2     21-DEC-2011     24-DEC-2011  
              2     25-JAN-2012     25-FEB-2012
              2     29-FEB-2012     25-MAR-2012


              and I need :  


             ID INTERVAL FREE
              1 15 DEC to 18 DEC
              2 24dec to 25 jan;25 feb to 29 feb
  • 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-15T13:55:40+00:00Added an answer on June 15, 2026 at 1:55 pm

    Here is SQLFiddle demo

    with t1 as
    (
    select t.*,
            row_number() over (order by date1) rn 
            from t
    )
    select t1.date1 as d1,t1.Date2 as d2 ,
    t2.Date2 as PreviousDate2,
    t1.Date1-t2.Date2 as DIff
    from t1
    left join t1 t2 on t1.rn=t2.rn+1
    order by t1.rn
    

    Here is a query to answer your edited question:

    SQLFiddle DEMO

    If you need to gather rows for each ID in one comma separated line you should do it on the client side not in SQL.

    with t1 as
    (
    select t.*,
            row_number() over (partition by id order by date1) rn 
            from t
    )
    select t1.id,
    t2.Date2 as PreviousDate2,
    t1.date1 as d1
    from t1
    left join t1 t2 on (t1.rn=t2.rn+1) and (t1.id=t2.id)
    where t2.Date2 is not null
    order by t1.id,t1.rn
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have different select boxes. I want to select second one with related id.
If I have 2 records with same information with difference in NAME of upper
I have different blocks of 34 threads each (0...33). I need to find a
I have some txt files that contain tables with a mix of different records
I have a table with column StartDate, I want to calculate the time difference
I have multiple records of the structure in the 'column1' column that I want
Have two sets of data (two tables) for patient records, one 1999-2003, the other
I have 4 tables (Type01, Type02, Type03, Type04) . They have different columns .
I have two tables, X and Y, with identical schema but different records. Given
I have different Bundles: MainBundle (Homepage), SecurityBundle (Login, Registration), MessageBundle (Message System), ShopBundle. I

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.