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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T16:57:01+00:00 2026-06-13T16:57:01+00:00

I have first table with defined work times: FreeTimes table (start datetime, end datetime)

  • 0

I have first table with defined work times: FreeTimes table (start datetime, end datetime) and second table with already planned tasks: TaskTime table (start datetime, end datetime).

I need somehow to subtract second table from first, so i get a result set of remainig FreeTimes under this conditions:

  • in case where TaskTime (or more TaskTimes) is in middle of FreeTime, i need to split FreeTime to time before task and time ramaining after task
  • in case when TaskTime is overlapping entire FreeTime i need to filter out this FreeTime
  • in case where TaskTime is intersecting with FreeTime i need to subtract TaskTime from FreeTime and leave only remaing part of FreeTime
  • 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-13T16:57:02+00:00Added an answer on June 13, 2026 at 4:57 pm

    The general way I’d solve this is as follows:

    ;With AllTimes as (
        select [Start] as EventTime from FreeTimes
        union
        select [End] from FreeTimes
        union
        select [Start] from TaskTimes
        union
        select [End] from TaskTimes
    ), OrderedTimes as (
        select EventTime,ROW_NUMBER() OVER (ORDER BY EventTime) rn
        from AllTimes
    ), Intervals as (
        select
            ot1.EventTime as StartTime,
            ot2.EventTime as EndTime
        from
            OrderedTimes ot1
                inner join
            OrderedTimes ot2
                on
                    ot1.rn = ot2.rn - 1
    )
    select * from Intervals i
    where not exists (
        select * from TaskTimes T where --Overlapped
           T.[Start] < i.EndTime and
           T.[End] > i.StartTime)
    and exists (
        select * from FreeTimes T where
           T.[Start] < i.EndTime and
           T.[End] > i.StartTime)
    

    Where we basically order all datetime values of interest, and then for each pair of successive values, work out if there’s some overlap with the TaskTimes table. If there is, then that pair shouldn’t be in the final result. (edit – we also have to check that the interval pair does actually overlap with FreeTimes too)

    You can, if needed, take this further and merge intervals (if there are overlapping rows in FreeTimes, you may end up with multiple intervals which are adjacent to each other)

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

Sidebar

Related Questions

I have two table first is TABLE_SUBJECT and second is TABLE_CHAPTER.Now i want to
I have 2 tables, let says the first table named Schedule and the second
I have a first table view and a segue that pushes to another table
I have first names stored in one table and last names stored in another.
i have 3 Tables is my SQL Database: First Table: Room: ID <pk>, Roomname
I have two tables. The first table has a column with a lot of
I have a DataSet , where in the first table and in the row
I have a performance problem on a query. First table is a Customer table
I have 2 tables which I want to query from. The first table (lets
I have a database framework where I have two tables. The first table has

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.