-1

Version: 10.2.18-MariaDB

I'm attempting to find the first available room within December (already included in the sample table) that will be free for 3 consecutive days.

The expected output should be:

Room 201, 2016-12-11

My question is essentially more about the syntax of SQL, should I be using the DATEDIFF function to determine the booking end date, or is there another method which I am not thinking of?

+--------------+--------+---------+--------------+
| booking_date | nights | room_no | arrival_time |
+--------------+--------+---------+--------------+
| 2016-12-01   |      2 |     201 | 17:00        |
| 2016-12-02   |      2 |     301 | 20:00        |
| 2016-12-03   |      5 |     101 | 16:00        |
| 2016-12-03   |      4 |     201 | 18:00        |
| 2016-12-04   |      1 |     301 | 15:00        |
| 2016-12-05   |      5 |     301 | 14:00        |
| 2016-12-07   |      4 |     201 | 20:00        |
| 2016-12-08   |      2 |     101 | 18:00        |
| 2016-12-10   |      5 |     101 | 23:00        |
| 2016-12-12   |      1 |     301 | 12:00        |
| 2016-12-15   |      3 |     101 | 19:00        |
+--------------+--------+---------+--------------+
5
  • Specify MySQL version.
    – Akina
    Nov 21, 2018 at 18:50
  • added the version @Akina
    – J S
    Nov 21, 2018 at 18:53
  • Well, your version knows about window functions and CTE. So you can prepare your data there. Build from-till pairs for each range when room is free (using LEAD function), then filter by the length you need. PS. I don't understand the correlation between your task and output fields in your query. PPS. Your query referres to room_type_requested field not shown in source data...
    – Akina
    Nov 21, 2018 at 19:19
  • @Akina I'm not familiar with SQL so that doesn't make an awful lot of sense. What don't you understand about the output and task? That is the next available date and room number, I just don't know how to achieve this through SQL. room_type_requested doesn't have any reference to my question.
    – J S
    Nov 21, 2018 at 19:45
  • What don't you understand about the output and task? You search for free range. So you do NOT need in all fields you select except room_no... in contrary you NEED in range start data, but you do not obtain it.
    – Akina
    Nov 21, 2018 at 19:50

1 Answer 1

1

Test:

WITH cte AS (
SELECT room_no, 
       booking_date + INTERVAL nights DAY d_start, 
       DATEDIFF(COALESCE(LEAD(booking_date) OVER (PARTITION BY room_no ORDER BY booking_date), 
                         '2100-01-01'), 
                booking_date + INTERVAL nights DAY) d_free
FROM booking
)
SELECT room_no, d_start
FROM cte
WHERE d_free >= 3
ORDER BY 2;

Output on test data is:

room_no d_start
201     2016-12-11
301     2016-12-13
101     2016-12-18

fiddle

4
  • This creates an error "booking.booking_date is not in GROUP BY." I appreciate the help however this seems to be over complicating the issue, is there not a more obvious solution or DATE function?
    – J S
    Nov 21, 2018 at 20:02
  • This creates an error "booking.booking_date is not in GROUP BY." Fiddle (MariaDB-10.2.17) shows the query is correct. Search the problem in another point.
    – Akina
    Nov 21, 2018 at 20:06
  • I'm on version 10.2.18 if that matters.
    – J S
    Nov 21, 2018 at 20:16
  • This versions have no differences which may affect on this query.
    – Akina
    Nov 21, 2018 at 20:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.