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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T14:55:04+00:00 2026-06-10T14:55:04+00:00

I have a table with bookings: id (int) quantity (tinyint) date_start (timestamp) date_end (timestamp)

  • 0

I have a table with bookings:

  • id (int)
  • quantity (tinyint)
  • date_start (timestamp)
  • date_end (timestamp)

Bookings can overlap, so there can for example be 20 bookings at the same time. Now I want to do two queries:

1) Retrieve the maximum sum of quantities within a given period.

2) Retrieve the minimum sum of quantities within a given period.

I have no idea how to add the quantities of ONLY the overlapping bookings together, and not of non-overlapping bookings. Any help would be greatly appreciated.

edit: i drew a picture for clarity: https://i.stack.imgur.com/I9nvq.png

  • 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-10T14:55:07+00:00Added an answer on June 10, 2026 at 2:55 pm
    1. Construct a list of all booking boundaries (i.e. start and end dates) which occur in the desired period:

        SELECT date_start AS boundary
        FROM   bookings
        WHERE  date_start BETWEEN @start AND @end
      UNION
        SELECT date_end
        FROM   bookings
        WHERE  date_end BETWEEN @start AND @end
      
    2. Add to that the boundary which occurs immediately before the desired period:

      -- [ from part 1 above ]
      UNION
        SELECT MAX(boundary)
        FROM (
          SELECT MAX(date_start) AS boundary
          FROM   bookings
          WHERE  date_start <= @start
        UNION ALL
          SELECT MAX(date_end)
          FROM   bookings
          WHERE  date_end <= @end
        ) t
      
    3. Make an outer join between this result and the bookings table, keeping all of the boundaries but only including a booking if it contributes to the number of simultaneous people after the boundary:

      FROM bookings RIGHT JOIN (
        -- [ from part 2 above ]
      ) t ON date_start <= boundary AND boundary < date_end
      
    4. Sum the number of people at each boundary:

      SELECT IFNULL(SUM(quantity),0) AS simultaneous_people
      -- [ from part 3 above ]
      GROUP BY boundary
      
    5. Find the maximum and minimum:

      SELECT MIN(simultaneous_people),
             MAX(simultaneous_people)
      FROM (
        -- [ from part 4 above ]
      ) t
      

    Putting it all together:

    SELECT MIN(simultaneous_people),
           MAX(simultaneous_people)
    FROM (
      SELECT IFNULL(SUM(quantity),0) AS simultaneous_people
      FROM   bookings RIGHT JOIN (
        SELECT date_start AS boundary
        FROM   bookings
        WHERE  date_start BETWEEN @start AND @end
      UNION
        SELECT date_end
        FROM   bookings
        WHERE  date_end BETWEEN @start AND @end
      UNION
        SELECT MAX(boundary)
        FROM (
          SELECT MAX(date_start) AS boundary
          FROM   bookings
          WHERE  date_start <= @start
        UNION ALL
          SELECT MAX(date_end)
          FROM   bookings
          WHERE  date_end <= @end
        ) t
      ) t ON date_start <= boundary AND boundary < date_end
      GROUP BY boundary
    ) t
    

    See it on sqlfiddle.

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

Sidebar

Related Questions

I have the following table SEASONS with time periods defining a booking prices for
I have table with around 70 000 rows. There is 6000 rows that i
I have table in which I am inserting rows for employee but next time
i have bookings table which has two people- i want to return person_1 as
I have a table rental: rentalId int(11) Customer_customerId int(11) Vehicle_registrationNumber varchar(20) startDate datetime endDate
I have 3 tables. cinema, booking and customer create table cinema ( c_id int,
I have a table of bookings. I want to count how many bookings occur
I have a table which stores bookings of rooms, the schema is: ID |
I have a two tables events and bookings, which are linked by event_id. Example:
In PostgreSQL 8.3 database I have bookings table referencing booking_transactions table by ID. So

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.