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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:08:01+00:00 2026-06-18T12:08:01+00:00

I have a query that identifies gaps and overlaps of date ranges in sql

  • 0

I have a query that identifies gaps and overlaps of date ranges in sql server 2008 r2. Each unique data set has 12 records. What I would like to do is to adjust or add to the code that identifies the gaps and overlaps and update the records to be sequential.

--gaps and overlaps tbl_volumes
with s as
(
select esiid,Read_Start,Read_End ,row_number() over(partition by esiid order by Read_Start) rn
from tbl_Volumes
where Status=0
group by esiid,Read_Start,Read_End)
select a.esiid, a.Read_Start, a.Read_End, b.Read_Start as nextstartdate,datediff(d,a.Read_End, b.Read_Start) as gap
into #go
from s a
join s b on b.esiid = a.esiid and b.rn = a.rn + 1
where datediff(d, a.Read_End, b.Read_Start) not in (0,1)
order by a.esiid

Here is the bad record set that I would like to see sequential:

e                   Read_Start  Read_End    Source
10032789402145965   2011-01-21  2011-02-22  867_03_1563303
10032789402145965   2011-02-22  2011-03-21  867_03_1665865
10032789402145965   2011-03-26  2011-04-20  867_03_1782993
  • 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-18T12:08:01+00:00Added an answer on June 18, 2026 at 12:08 pm

    Well, you could just assign a new Read_end to each record based on the next value. The calculation for the new start can be done like this:

    select t.*,
           (select top 1 Read_Start
            from t t2
            where t2.e = t.e and t2.Read_Start > t.Read_Start
            order by t2.Read_Start
           ) as New_Read_End
    from t
    

    Do you actually want to update the value or just see what it should be?

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

Sidebar

Related Questions

I have a SQL Server 2005 database that stores data for multiple users. Each
I have a query that successfully grabs the unique products from my products table
I have a query that creates a result set like this: Rank Name 1
I have a query that looks like this SELECT * from myTable WHERE Date
I have this query that should: create a listing that identifies different items in
I have an SQL query that I would like to convert into a criteria
I have some data in a table that looks similar to this: Item Date
I have a fairly complex SQL query that pulls different types of products from
Ok so we have some tables in SQL that identifies users by their Guid
I have a hypothetical set of data with 3 columns that has monthly profit

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.