You need to write an SQL query for the booking system
Hello!
I have a table with bookings for some rooms:
CREATE TABLE `booking` ( `id` int(11) NOT NULL, `room_id` int(11) DEFAULT NULL, `client_id` int(11) DEFAULT NULL, `timestamp_from` int(11) DEFAULT NULL, `timestamp_to` int(11) DEFAULT NULL, `status_id` int(11) DEFAULT NULL, `guests` int(11) DEFAULT NULL, `type` int(11) DEFAULT NULL, `tariff` int(11) DEFAULT NULL, `comment` text DEFAULT NULL, `created_by` int(11) DEFAULT NULL, `updated_by` int(11) DEFAULT NULL, `created_at` int(11) DEFAULT NULL, `updated_at` int(11) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
also a table of statuses:
CREATE TABLE `booking_status` ( `id` int(11) NOT NULL, `booking_id` int(11) DEFAULT NULL, `status` int(11) DEFAULT NULL, `created_by` int(11) DEFAULT NULL, `updated_by` int(11) DEFAULT NULL, `created_at` int(11) DEFAULT NULL, `updated_at` int(11) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
The task is as follows:
I need an SQL query that would take parameters:
- timestamp_from - start time of search
- timestamp_to - end time of search
- duration - duration (in seconds)
- slot_interval - slot interval (in seconds)
- by_room - whether to consider rooms
In response, the query should return all possible combinations of available "slots" for booking (available - the time for which there is currently no active booking for a specific room).
A slot is a time interval with a duration of duration, which has a start time, an end time, and a room number.
It should be noted that there are many rooms, and it is not necessary that if there is a booking record, all rooms are occupied at that time.
Also, it should be taken into account that bookings can be deleted (booking_status.status = 0), in which case we consider it as available time, and slots can be generated for it.
And importantly, if there is already an active booking, the slots should be generated taking into account a 30-minute interval before and after the already active booking.
The by_room parameter determines whether to return a specific room or not. If yes, the query should return all slots for each room separately. If not, the query should return completely available slots (for example, if there is a booking for one room at a certain time, but not for another - the query should return that slot as available)
Example (considering that we have only ONE room, and no others):
I have two bookings for ONE room:
1) 20.05.2024 10:00 - 20.05.2024 14:00
2) 20.05.2024 18:00 - 20.05.2024 22:00
I enter the following data in the query:
timestamp_from - 20.05.2024 10:00 (naturally in timestamp)
timestamp_to - 20.05.2024 23:00 (naturally in timestamp)
duration - 7200 (2 hours in seconds)
slot_interval - 1800 (30 minutes)
by_room - true
In response, I should get such an array (all dates should be in timestamps!!!):
room_id: 1, start: 20.05.2024 14:30, end: 20.05.2024 16:30;
room_id: 1, start: 20.05.2024 15:00, end: 20.05.2024 17:00;
room_id: 1, start: 20.05.2024 15:30, end: 20.05.2024 17:30;
If there was a deleted booking in any of these intervals, it would not be considered!!!!
If we had more rooms available, and there were no bookings on them - there would be plenty of slots available for the whole day.
Payment will be made after all tests from my side and adjustments from the performer's side.
The use of various AI is not allowed.
-
91 1 0 Good day! I have over 20 years of experience in developing similar tasks. I am ready to help after reviewing the structure of the tables and the technical task for which all this was done. Judging by the requirements, it seems that you have issues with the structure and lack a field or another table...
-
456 1 0 Good day
I know SQL well, I can do the task, but I will need a database dump, and which database specifically (MySql, Sql Server, Oracle etc?)
Contact me.
-
257 19 0 3 Good day, there is a ready script for a booking app, I am currently working on it, it needs to be adjusted to your database, but overall it is fully functional, written in JS, I can write it in another language, using pure SQL would result in a too long query, tested. Contact me, we will agree on the script and I will answer any other questions.
-
1323 35 1 Good day. I am well-versed in SQL. I would like to see the table dump, I will take the input data from the specification. I will make the query and send the result.
-
2398 39 1 Good day.
Write to me, let's discuss your issue in more detail.
I think I can solve your problem.
-
В состоянии проверить - в состоянии сделать. Могу себе представить, чем могут окончиться эти простейшие упражнения. Час-два, конечно, не время.
-
У вас есть заполненные таблицы?
-
А какой клиент был в комнате, где room_id = 1
20.05.2024 в 16:00 из вашего примера?
Там вроде 3 брони на это время... -
> Нужно учесть, что комнат много, и необязательно, что если существует запись про бронь, то на это время заняты все комнаты.
ну і як це знати? наприклад у вас є 4 кімнати. для 3х з них в системі немає вільних слотів для заданого інтервалу. а для 4 нічого немає. тобто фізично вона у вас є, а "в системі" (цих 2х таблицях що ви привели) вона взагалі не світиться.
то як це знати?
-
Доброго дня.
На чому написано ваш сайт? -
Current freelance projects in the category Databases & SQL
Database parsing
45 USD
Channel requirements: 1. Content language: Russian or Ukrainian (mixed RU/UA content is allowed) 2. Number of subscribers: At least 500 subscribers 3. Activity: The last post published no later than 32 hours ago 4. Comments: Comments must be open under the posts (through a group… Databases & SQL, Data Parsing ∙ 15 hours 28 minutes back ∙ 27 proposals |
It is necessary to check the scripts and update the data in the Postgres database.It is necessary to correct the SQL scripts for the Postgres database. It is required to check the scripts and update data from external Excel tables and between two Postgres databases (different servers). Scripts will be run through AnyDesk using Navicat. List of data for… Databases & SQL ∙ 3 days 18 hours back ∙ 18 proposals |
Need an Airtable architect to build a relational schema and a new clean Airtable base.Need help rethinking and building a clean relational schema for an internal operational system on Airtable. The current database is already in use by the team, but it has grown organically: the structure is partially flat, some tables/views are actively used, while others are… Databases & SQL, Desktop Apps ∙ 4 days 11 hours back ∙ 10 proposals |
Basketball Coaching Education Platform + Custom CMSBasketball Coaching Education Platform + Custom CMSProject Overview We are looking for an experienced web development team or full-stack developer to build a modern basketball coaching education platform. The website will provide basketball coaches with access to educational… Databases & SQL, Web Programming ∙ 5 days 5 hours back ∙ 92 proposals |
Integration of Viber in 8.3
223 USD
Need Viber integration into own CRM (1C 8.3)About the Company The company "Domofon System" is engaged in the installation and maintenance of intercom systems. Base of over 40,000 subscribers. We work on our own customized system based on 1C 8.3. We are looking for a specialist… Databases & SQL, Bot Development ∙ 5 days 8 hours back ∙ 18 proposals |