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

The Archive Base Latest Questions

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

I hope this question provides all of the necessary information, but please do request

  • 0

I hope this question provides all of the necessary information, but please do request more if anything is unclear. This is my first question on stack overflow so please bear with me.

I am running this query on SQL Server 2005.

I have a large derived dataset (i’ll provide a small subset later) which has 4 fields;
ID,
Year,
StartDate,
EndDate

Within this data set the ID may (correctly) appear multiple times with different date combinations.

The question I have is what ways are there to identify if a record is ‘new’ I.E it’s start date does not fall between the start and end date of any other records for the same id.

For an example take the data set below (I hope this table comes out correctly!);

+----+------+------------+------------+
| ID | Year | Start Date |  End Date  |
+----+------+------------+------------+
|  1 | 2007 | 01/01/2007 | 10/10/2007 |
|  1 | 2007 | 01/01/2007 | 05/04/2007 |
|  1 | 2007 | 05/04/2007 | 08/10/2007 |
|  1 | 2007 | 15/10/2007 | 20/10/2007 |
|  1 | 2007 | 25/10/2007 | 01/01/2008 |
|  2 | 2007 | 01/01/2007 | 01/01/2008 |
|  2 | 2008 | 01/01/2008 | 15/07/2008 |
|  2 | 2008 | 10/06/2008 | 01/01/2009 |
+----+------+------------+------------+

If we say nothing existed before 2007 then Row 1 and Row 6 are ‘new’ at that time.

Rows 2,3,7 and 8 are not ‘new’ as they either join the end of a previous record or overlap it to form a continuous date period (take rows 6 and 7 there are no ‘breaks’ between 01/01/2008 and 01/01/2009)

Row 4 and 5 would be considered a new record as it does not attach directly to the end of the previous period for ID 1 or overlap any of the other periods.

Currently to get this data set I have to put all of my data into temporary tables and then join them together on various fields to remove the records I don’t want.

Firstly I remove rows where the startdate equals the enddate of another row for that ID (This would get rid of rows 3 and 7)

Then I remove rows where the the start date is between the startdate and enddate of other records for that ID (this would remove rows 2 and 8)

That would leave me withRows 1,4,5 and 6 as the ‘new’ records which is correct.

Is there a more efficient way to do this such as in some sort of loop, CTE or cough Cursor?

As per the above, if there is anything unclear don’t hesitate to ask and I will try and provide you with the information you request.

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

    Try

    ;with cte as
    (
        Select *, row_number() over (partition by id order by startdate) rn from yourtable
    )
    select distinct t1.* 
    from cte t1
         left join cte t2 
         on t1.ID = t2.ID
         and t1.EndDate>=t2.StartDate and t1.StartDate<=t2.EndDate
         and t1.rn<>t2.rn
    where t2.ID is null
    or t1.rn=1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I hope this question is not a duplicate but I didn't find anything equivalent
I hope this question isn't too open ended, but a nudge in the right
I hope this question will not be rejected as there is no code. But
This is my first time here so I hope I post this question at
I have asked this question earlier today but I didn't provide enough information and
first of all, I hope that I ask the question in the right context
Usless Background Info Hello, all. This is my first post here, but I often
First of all; I'm aware that this question is strange and I will probably
Hope this question is not stupid since I am an amateur web designer. I
I hope this question isn't too general. Well, the situation is that I am

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.