简体   繁体   中英

MySQL Hotel Room total fee query based on dates

I need to query a small hotel reservation system and I need help with the a query to get the rooms available on a specific set of dates with the total fees for the room.

The table looks like this:

+--------+------------+-------+--------+-------+------------------+
| RoomId | Date       | ResID |  Rate  | Tax   | DailyCleaningFee |
+--------+------------+-------+--------+-------+------------------+
|    1   | 2019-01-01 |   0   | 100    | 0.130 |    30            |
+--------+------------+-------+--------+-------+------------------+

ResID determines if the room is booked on certain dates or not. 0 being room available. I want to query all rooms available for 2 nights on specific cates, and it should also return Total rate for the room including cleaning fee and taxes resulting in a table like this:

+---------+------------+
| RoomId  | TotalRate |
+-------- +------------+

If I want to query my table for 2 nights, 2019-01-02 and 2019-01-03, it should also exclude records where Tax is null and/or if the rate is set to 0. What would my query look like?

Really grateful for any help I can get. Thank you!

Edit: I added a DB Fiddle link to an example of the table.

Your question is missing some information to answer it accurately. But I think I can help you to understand what your query should look like to get the expected result. See below.

SELECT DISTINCT
   t1.RoomId AS RoomId, 
   (((t1.Rate + t1.DailyCleaningFee) * (1.0 + t1.Tax)) + ((t2.Rate + t2.DailyCleaningFee) * (1.0 + t2.Tax))) AS TotalRate  -- total rate has been the sum of each day rate + tax
FROM (
    SELECT h.*
    FROM `Hotel` h
    WHERE h.Date = '2018-12-02'   -- your first date goes here
    AND h.ResID = 0 AND h.Tax IS NOT NULL AND h.Rate > 0
) t1
INNER JOIN (
    SELECT h.*
    FROM `Hotel` h
    WHERE h.Date = '2018-12-03'  -- your second date goes here
    AND h.ResID = 0 AND h.Tax IS NOT NULL AND h.Rate > 0
) t2 ON t1.RoomId = t2.RoomId

This works, because, for each date you are searching (here you mentioned for two days), you need to have an inner query, and then you need to join all together by RoomId to collect rooms available in all of provided dates. After then only you can calculate the final rate.

You may have to modify tax calculation formula which you have not clearly mentioned.


Edit: (2018-12-01)

Above query valid only explicitly for two days.

To calculate total rate for any number of continuous days , you can use following query.

SELECT
    t1.RoomId AS RoomId,
    ROUND(SUM(t1.DailyRate), 2) AS TotalRate
FROM (
    SELECT
        h.RoomId,
        ((h.Rate + h.DailyCleaningFee) * (1.0 + h.Tax)) AS DailyRate
    FROM Hotel h
    WHERE
        h.ResID = 0 
        AND h.Tax IS NOT NULL 
        AND h.Rate > 0
        AND h.Date >= '2018-12-02' AND h.Date <= '2018-12-04'
) t1
GROUP BY
    t1.RoomId
HAVING
    COUNT(*) >= DATEDIFF('2018-12-04', '2018-12-02') + 1;  

Note that the dates are inclusive in both boundaries. Replace hardcoded dates with your date parameters. In HAVING clause we check whether there are continuous days available from a single room for all days, and only filters them.

According to @Strawberry it might not be the efficient way to store room charges in a kind of given table. You may need to change the schema little bit to make it gain more performance.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM