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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T07:32:51+00:00 2026-05-13T07:32:51+00:00

I have two tables both of which have columns StartDate and EndDate. I’m trying

  • 0

I have two tables both of which have columns StartDate and EndDate.

I’m trying to return a single resultset that contains all date ranges from one table (TableA), and all complement date ranges from the other one (TableB).

CREATE TABLE [dbo].[TableA](
    [ID] [int] NOT NULL,
    [StartDate] [datetime] NOT NULL,
    [EndDate] [datetime] NOT NULL
)

CREATE TABLE [dbo].[TableB](
    [ID] [int] NOT NULL,
    [StartDate] [datetime] NOT NULL,
    [EndDate] [datetime] NOT NULL
)

INSERT INTO TableA (ID, StartDate, EndDate) VALUES(1, '4/1/2009', '8/1/2009')
INSERT INTO TableA (ID, StartDate, EndDate) VALUES(1, '10/1/2009', '12/1/2009')
INSERT INTO TableB (ID, StartDate, EndDate) VALUES(1, '1/1/2009', '2/1/2010')

INSERT INTO TableA (ID, StartDate, EndDate) VALUES(2, '4/1/2009', '8/1/2009')
INSERT INTO TableB (ID, StartDate, EndDate) VALUES(2, '1/1/2009', '5/1/2009')
INSERT INTO TableB (ID, StartDate, EndDate) VALUES(2, '7/1/2009', '12/1/2009')

The expected resultset from the three datasets should be:

(ID = 1)
1/1/2009 - 4/1/2009 (from TableB)
4/1/2009 - 8/1/2009 (from TableA)
8/1/2009 - 10/1/2009 (from TableB)
10/1/2009 - 12/1/2009 (from TableA)
12/1/2009 - 2/1/2010 (from TableB)

(ID = 2)
1/1/2009 - 4/1/2009 (from TableB)
4/1/2009 - 8/1/2009 (from TableA)
8/1/2009 - 12/1/2009 (from TableB)

The date ranges are not guaranteed to be continuous, and I can’t make any assumptions on how they’re overlapping between tables…within each table they can be assumed to not overlap.

I’m having problems wrapping my head around how to split the single date ranges in TableB into multiple pieces to find all the complement “regions” within it in SQL.

Anyone have any suggestions?

  • 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-13T07:32:51+00:00Added an answer on May 13, 2026 at 7:32 am

    If you create this as a view, I think it does what you want. It uses CTEs, which should be supported by SQL Server 2005, but not earlier.

    WITH Timestamps AS (
        SELECT Id, StartDate AS Date FROM TableA
        UNION
        SELECT Id, EndDate AS Date FROM TableA
        UNION
        SELECT Id, StartDate AS Date FROM TableB
        UNION
        SELECT Id, EndDate AS Date FROM TableB
    ), Timestamps2 AS (
        SELECT ROW_NUMBER() OVER (ORDER BY Id, Date) AS RowNumber, * FROM Timestamps
    ), Timestamps3 AS (
        SELECT T1.ID, T1.Date AS StartDate, T2.Date AS EndDate
        FROM Timestamps2 AS T1 JOIN Timestamps2 AS T2
        ON T1.RowNumber + 1 = T2.RowNumber AND T1.ID = T2.ID
    ), IntervalsFromB AS (
        SELECT T.ID, T.StartDate, T.EndDate FROM Timestamps3 AS T
        LEFT JOIN TableA AS A
        ON T.StartDate >= A.StartDate AND T.EndDate <= A.EndDate
        WHERE A.StartDate IS NULL)
    SELECT * FROM TableA
    UNION ALL
    SELECT * FROM IntervalsFromB
    

    Full output (ordered by Id, StartDate for readability):

    Id  StartDate               EndDate
    1   2009-01-01 00:00:00.000 2009-04-01 00:00:00.000
    1   2009-04-01 00:00:00.000 2009-08-01 00:00:00.000
    1   2009-08-01 00:00:00.000 2009-10-01 00:00:00.000
    1   2009-10-01 00:00:00.000 2009-12-01 00:00:00.000
    1   2009-12-01 00:00:00.000 2010-02-01 00:00:00.000
    2   2009-01-01 00:00:00.000 2009-04-01 00:00:00.000
    2   2009-04-01 00:00:00.000 2009-08-01 00:00:00.000
    2   2009-08-01 00:00:00.000 2009-12-01 00:00:00.000
    

    It was pretty complicated for me to implement this, so I’m wondering if anyone can see a simpler way. I might be missing some trick that makes this much simpler. If so, please let me know! Also, you will almost certainly need some indexes on your tables to get this to perform well if you have a lot of rows. Some other optimizations may be possible – I haven’t tried for the fastest possible performance, but just to get the correct result.

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

Sidebar

Related Questions

I have two tables invoices and pending_payments both of which have the following rows
I have two tables, Users and People, both of which share a common attribute,
We have two tables in our application that both have a ShowOrder column. We
I have two tables and need to retrieve some columns from both tables based
I have two tables which both have a 'Name' column. The tables are not
I have two tables in my database - Category and Department which both contain
i have two tables i my database in which i have two columns in
i have two tables both are related with primary-foreign key ralation and i have
I have two tables. Both tables are editable and both tables should allways be
I have two tables, both named as say, Employee in two different schema HR

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.