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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T11:28:41+00:00 2026-06-07T11:28:41+00:00

I have a table of available date blocks (7 days in my case) which

  • 0

I have a table of available date blocks (7 days in my case) which may or may not be consecutive:

start_date    end_date    booked    id    room_id

2012-07-14    2012-07-21    0       1        6
2012-07-21    2012-07-28    0       2        6
2012-07-28    2012-08-04    1       3        6
2012-08-04    2012-08-11    0       4        6

What I’d like to do is be able to get a result set that gives me one row per X weeks of consecutive unbooked dates, within a date range.

So, for 2 week blocks starting on the 14th of July and using the above table data, I would expect the following:

start_date    end_date    booked
2012-07-14    2012-07-28    0

The second block of 2 weeks would not be returned as one of the component weeks is booked.

Here are a few ideas I’ve tried already:

SELECT 
    MIN(start_date) AS start_date_min,
    MAX(end_date) AS end_date_max,
    CAST(GROUP_CONCAT(id) AS CHAR) AS ids,
    SUM(booked) AS booked
FROM 
    available_dates
WHERE
    (start_date>=20120714 AND end_date<=DATE_ADD(20120714, INTERVAL 14 DAY))
GROUP BY
    room_id
HAVING
    end_date_max=DATE_ADD(20120714, INTERVAL 14 DAY)

This gets me part of the way, however doesn’t get me the consecutive results – that is the important part. It also only returns a single result (probably because of the HAVING clause) when I widen the test data.

Can anyone point me in the right direction?

  • 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-07T11:28:44+00:00Added an answer on June 7, 2026 at 11:28 am

    If you have a calendar or a numbers table:

    CREATE TABLE num
    ( i INT NOT NULL
    , PRIMARY KEY (i)
    ) ;
    
    INSERT INTO num 
      (i) 
    VALUES
      (0), (1), (2), ..., (1000) ;
    

    You could use something like this:

    SELECT 
        avail.room_id,
        MIN(avail.start_date) AS start_date_min,
        MAX(avail.end_date) AS end_date_max,
        CAST(GROUP_CONCAT(avail.id) AS CHAR) AS ids,
        SUM(avail.booked) AS booked
    FROM 
        available_dates AS avail
      CROSS JOIN
        ( SELECT DATE('2012-07-14') AS start_date_check
               , 52                 AS max_week_check
        ) AS param
      JOIN
        num
          ON  avail.start_date = param.start_date_check + INTERVAL num.i WEEK
          AND num.i < param.max_week_check
    WHERE
        avail.booked = 0
    GROUP BY
        avail.room_id,
        ( num.i / 2 )        
    HAVING
        COUNT(*) = 2
    

    You could also have this:

    WHERE
        1 =1                         --- no WHERE condition
    GROUP BY
        avail.room_id,
        ( num.i / 2 )        
    HAVING                           --- and optionally
        SUM(avail.booked) = 0        --- this
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have table table1 **************************** id | start_date | end_date 1 30/06/2012 01/01/9999 When
I have a html table which consists of columns for days, and rows for
I have a table called 'holiday table' which basically contains dates for all days
I have a stored procedure which I use to return the earliest available date
We have a table which has a DATE column d. I'd like to get
I have table in which I am inserting rows for employee but next time
I have a table: Date ColumnA ColumnB 2009-12-29 abc abc 2009-12-29 abc abc 2009-12-29
I have a table with multiple date columns per employee: Emp 1, Date1, Date2,
I have a table which stores bookings of rooms, the schema is: ID |
I have a table in mysql that holds a start and end date. These

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.