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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T19:33:02+00:00 2026-05-12T19:33:02+00:00

In an SQL table I keep bookings for various resouces, with a StartDate/EndDate column:

  • 0

In an SQL table I keep bookings for various resouces, with a StartDate/EndDate column:

ResourceID,  StartDate,   EndDate
-----------------------------------
1          2009-01-01    2009-01-05
1          2009-01-07    2009-01-10
2          2009-01-03    2009-01-18

I need to produce a list of all resources that are available for at least X consecutive days in a given time interval: ie from 2009-01-01 to 2009-01-20 and X= 5 consecutive days.

eg. for ResourceID=1 ,the biggest available time interval is 2009-01-10 =>2009-01-20 so this qualifies, but for ResourceID=2 there is no 5-days available slot. there is an available one from 2009-01-01 => 2009-01-03 and another at 2009-01-18=> 2009-01-20 , neither long enough

EDIT:

Based on the answer from Quassnoi, I’ve added some modifications to handle some edge cases: resources w/out reservations, or reservations that span over the entire lookup period.

This looks like the final result. Muchos gracias for all the help !

WITH   avRows AS        (         
     SELECT  a.ID as aid,startDate , endDate , ROW_NUMBER() OVER (PARTITION BY ResourceID ORDER BY endDate ) AS rn   
     FROM    tblResources a left outer join tblReservations  b          
     on b.ResourceID = a.ID and (startDate  BETWEEN '2009-01-01' AND '2009-01-20'  OR endDate  BETWEEN '2009-01-01' AND '2009-01-20' )
     where a.ID NOT IN (select distinct ResourceID from tblReservations where    (startDate  <'2009-01-01'   AND  endDate  > '2009-01-20' ))
     )SELECT  DISTINCT COALESCE(rs.aid, rp.aid)

      FROM   avRows rs FULL JOIN    avRows rp ON   rs.aid = rp.aid  AND rp.rn = rs.rn - 1 
      WHERE   DATEDIFF(day, COALESCE(rp.endDate , '2009-01-01'), COALESCE(rs.startDate , '2009-01-20')) >= 5
  • 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-12T19:33:03+00:00Added an answer on May 12, 2026 at 7:33 pm
    SELECT  resourceID
    FROM    mytable
    WHERE   startDate BETWEEN '2009-01-01' AND '2009-01-20'
            AND DATEDIFF(day, CASE WHEN endDate < '2009-01-20' THEN endDate ELSE '2009-01-20' END, startDate) >= 5
    UNION
    SELECT  resourceID
    FROM    mytable
    WHERE   endDate BETWEEN '2009-01-01' AND '2009-01-20'
            AND DATEDIFF(day, endDate, CASE WHEN startDate > '2009-01-01' THEN startDate ELSE '2009-01-01' END) >= 5
    

    Update:

    Sorry, didn’t notice your records mean busy resources, not free resources.

    Try this:

    WITH    rows AS
            (
            SELECT  ResourceID, StartDate, EndDate, ROW_NUMBER() OVER (PARTITION BY ResourceID ORDER BY EndDate) AS rn
            FROM    mytable
            WHERE   StartDate BETWEEN '2009-01-01' AND '2009-01-20'
                    AND EndDate BETWEEN '2009-01-01' AND '2009-01-20'
            )
    SELECT  DISTINCT COALESCE(rs.ResourceID, rp.ResourceID)
    FROM    rows rs
    FULL JOIN
            rows rp
    ON      rs.ResourceID = rp.ResourceID
            AND rp.rn = rs.rn - 1
    WHERE   DATEDIFF(day, COALESCE(rp.EndDate, '2009-01-01'), COALESCE(rs.StartDate, '2009-01-20')) >= 5
    

    Update 2:

    More detailed explanations and performance tests:

    • Inverting date ranges (works in SQL Server, Oracle and PostgreSQL 8.4)
    • Inverting date ranges: MySQL (works in MySQL)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've a sql table in which one of the column 'Codes' consists of multiple
I am using a SQL server table to keep related information with a URL
I've got an SQL table that I use to keep product data. Some products
I am attempting to keep a table in Sql server updated from an access
To keep things very simple, say I have a SQL Server 2008 table with
SQL: Create new table for keep a definition of status in other table OR
I need to keep a table in sync with a recursive CTE query that
What's the best way to delete all rows from a table in sql but
Basically, I have a list of dates in a SQL table with some statistics
My sql table is something like (message,created) I want to select those rows which

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.