This article is a mirror article of machine translation, please click here to jump to the original article.

View: 23250|Reply: 11

[New Issue] How to choose the cheapest n rooms

[Copy link]
Posted on 12/5/2018 9:32:38 PM | | | |
Now there are two tables to use, room_type and room_info
There are 3 hotels with IDs 1~3. Then each hotel has 3 room types, with IDs of 1~9 respectively.
The room_type contains the name, ID, and corresponding hotel ID of each room type
What is stored in the room_info is the price of each day, each room type, and the number of rooms remaining.
The topics I would like to ask are:
Specify the time range and the number of rooms to be booked, query the hotels, room type selections and their average prices that meet the conditions (time, number of rooms remaining), and sort them by average price from lowest to highest. The results include hotels, selected room combinations and quantities, and the lowest average price.
##不要求为同一房型, but it must be the same hotel, and you can change the room type midway.
##比如, the check-in time is 2018-11-14~2018-11-15, and the number of rooms booked is 5
##返回选择酒店A, 11-14 select 4 room types with ID 3, 1 room type with ID 2, 11-15 select 3 room types with ID 3, 2 room types with ID 2, and the lowest price is 2000
##老师要求只能用sql语句, MySQL implementation cannot use Python, C++, etc
I didn't write it out for a day, and I didn't see a similar topic on the Internet, so I came to ask the gods for help, and I want to ask you how to achieve it, thank you~







Previous:PHP program ape's self-reported experience sharing
Next:2019 postgraduate entrance examination politics Xu Tao and Xiao Xiurong's sprint materials
Posted on 12/6/2018 5:00:58 PM |
This post was last edited by Miaolinsen on 2018-12-6 17:03

I didn't do it with mysql, I did it with mssql:


/*
Specify the time range and the number of rooms to be booked, query the hotels, room type selections and their average prices that meet the conditions (time, number of rooms remaining), and sort them by average price from lowest to highest. The results include hotels, selected room combinations and quantities, and the lowest average price.
##不要求为同一房型, but it must be the same hotel, and you can change the room type midway. -- Parsing: It means that there is no need to consider the room type, only the number of rooms is considered
##比如, select the check-in time 2018-11-14~2018-11-15, and the number of rooms booked is 5 -- Analysis: The hotel needs to be in the date range, and there is a demand for the number of rooms every day (if there is no one on a certain day, it will not meet the standard)
##返回选择酒店A, 11-14 select 4 room types with ID 3, 1 room type with ID 2, 11-15 select 3 room types with ID 3, 2 room types with ID 2, and the lowest price is 2000 -- Analysis: Calculate the average price according to the minimum configuration per day
##老师要求只能用sql语句, mysql implementation, cannot use python, c++, etc. */


-- Incoming parameters date range, number of rooms required
DECLARE @sdate DATE = '2018-11-14';
DECLARE @edate DATE = '2018-11-15';
DECLARE @roomnum INT = 5;


-- Calculate the number of days of stay
DECLARE @days INT;
SET @days = DATEDIFF(DAY, @sdate, @edate)+1;

-- Hotel number that meets the date range, number of rooms
IF    EXISTS(select * from tempdb.dbo.sysobjects where id = object_id(N'tempdb.. ##temp_hotel') and type='U')
BEGIN
    drop table ##temp_hotel
END

SELECT ta.hotel_id into ##temp_hotel FROM
(SELECT ri. [date], rt.hotel_id, SUM(ri.remain) room_num  FROM room_info ri LEFT JOIN room_type rt on ri.room_id=rt.room_id where @sdate <= [date] and [date] <= @edate GROUP BY ri. [date],rt.hotel_id HAVING SUM(remain) >= 5) ta
GROUP BY ta.hotel_id HAVING COUNT(*) = @days



-- Calculated according to the hotel (configure the minimum room type per day)

IF    EXISTS(select * from tempdb.dbo.sysobjects where id = object_id(N'tempdb.. ##temp_result') and type='U')
BEGIN
    drop table ##temp_result
END
-- Provisional Table of Results Hotel Date Room Type ID Room Type Name Number of Rooms Room Price Hotel Daily Total Hotel Daily Average Hotel Price Hotel Total Hotel Price Hotel Total Daily Average Price of Hotel Total Daily Room Price
CREATE TABLE ##temp_result(
    hotel_id INT,
    hotel_name varchar(255),
    [date] DATE,
    room_id INT,
    room_name VARCHAR(255),
    room_num INT,
    roomprice DECIMAL,
    dsumprice DECIMAL,
    davgprice DECIMAL,
    sumpric DECIMAL,
    sumavgpric DECIMAL
)

DECLARE @hid INT; -- Hotel ID
DECLARE @infoid INT; -- info id
--affirm that the cursor is a collection
DECLARE my_cursor cursor for (SELECT hotel_id from ##temp_hotel);
--Open cursor--
open my_cursor
--Start Loop Cursor Variables--
fetch next from my_cursor into @hid
while @@FETCH_STATUS = 0 -- returns the state of the last cursor executed by the FETCH statement --
BEGIN      
    -- Based on the hotel ID, the recurring date is priced
    IF    EXISTS(select * from tempdb.dbo.sysobjects where id = object_id(N'tempdb.. ##temp_room') and type='U')
    BEGIN
        drop table ##temp_room
    END

    -- Hotels, by date, with rooms sorted by date Rate
    SELECT info_id, [date], price, remain, room_id INTO ##temp_room FROM dbo.room_info WHERE @sdate <= [date] and [date] <= @edate AND remain > 0 AND room_id IN (SELECT rt.room_id FROM dbo.room_type rt WHERE rt.hotel_id=@hid) ORDER BY [date] ASC, price ASC
   
   
    DECLARE @thisrdate DATE = NULL; -- Date
    DECLARE @thisrnum INT=0; -- Number of rooms selected on the current date
   
    DECLARE @rdate DATE = NULL; -- Date
    DECLARE @rmnum INT = 0; -- Number of rooms by room type
   
    -- Cycle by date
    DECLARE @my_cursor2 CURSOR;
    set @my_cursor2 = CURSOR FOR SELECT info_id FROM ##temp_room ORDER BY [date] ASC, price ASC
    OPEN @my_cursor2;
    FETCH NEXT FROM @my_cursor2 INTO @infoid
    WHILE(@@FETCH_STATUS=0)
    BEGIN
        --##temp_result
        SELECT @rmnum=remain, @rdate=[date] FROM ##temp_room WHERE info_id=@infoid
        IF (@thisrdate IS NULL OR @thisrdate != @rdate)
        BEGIN
            PRINT @rdate
            SET @thisrdate = @rdate;
            SET @thisrnum = 0;
        END
        -- Dosage form selection room type
        IF(@thisrnum <= @roomnum)
        BEGIN
            IF (@thisrnum + @rmnum  > @roomnum)
            BEGIN
                -- The number of rooms you need to use
                SET @rmnum = @roomnum-@thisrnum;
            END
        END
        ELSE   
        BEGIN
            -- No room is required for that date
            SET @rmnum = 0;
        END
        
        IF (@rmnum > 0)
        BEGIN
            -- Add results by specified number
            INSERT INTO ##temp_result(hotel_id, hotel_name, [date], room_id, room_name, room_num, roomprice, dsumprice, davgprice, sumpric, sumavgpric)
            SELECT @hid, '', @thisrdate, room_id, '', @rmnum, price, 0, 0, 0, 0 FROM ##temp_room WHERE info_id = @infoid
            
            -- The number of rooms has reached the calculated average price
            SET @thisrnum = @thisrnum + @rmnum;
            IF    (@thisrnum = @roomnum)
            BEGIN
                -- Total price by hotel, calculated for dates
                UPDATE ##temp_result SET dsumprice=(SELECT SUM(room_num*roomprice) FROM ##temp_result tr WHERE tr.hotel_id=@hid AND tr.[ date]=@thisrdate) WHERE hotel_id=@hid AND [date]=@thisrdate
                -- Calculate the average room rate by hotel, total price by date
                UPDATE ##temp_result SET davgprice=(dsumprice/@roomnum) WHERE hotel_id=@hid AND [date]=@thisrdate
            END
        END

        FETCH NEXT FROM @my_cursor2 INTO @infoid;
    END   
    close @my_cursor2;  --Turn off the cursor
    deallocate @my_cursor2;   --release cursor

    -- By hotel, calculate the total hotel price
    UPDATE ##temp_result SET sumpric=(SELECT SUM(room_num*roomprice) FROM ##temp_result tr WHERE tr.hotel_id=@hid) WHERE hotel_id=@hid
    -- Calculate the average daily price of the total room in the hotel
    UPDATE ##temp_result SET sumavgpric=(sumpric/@roomnum/@days) WHERE hotel_id=@hid

    FETCH next from my_cursor into @hid - go to the next cursor, no deadloops
END  
close my_cursor -- close the cursor
deallocate my_cursor - release the cursor


-- Add the hotel name and room type name to the temporary table
UPDATE ##temp_result SET hotel_name=(SELECT ht.hotel_name FROM dbo.hotel ht WHERE ht.hotel_id=##temp_result.hotel_id),
room_name=(SELECT rt.room_name FROM dbo.room_type rt WHERE rt.room_id=##temp_result.room_id)


-- Check the results from the results table
SELECT * FROM ##temp_result




-- End clearing temporary tables
IF    EXISTS(select * from tempdb.dbo.sysobjects where id = object_id(N'tempdb.. ##temp_hotel') and type='U')
BEGIN
    drop table ##temp_hotel
END
IF    EXISTS(select * from tempdb.dbo.sysobjects where id = object_id(N'tempdb.. ##temp_room') and type='U')
BEGIN
    drop table ##temp_room
END
IF    EXISTS(select * from tempdb.dbo.sysobjects where id = object_id(N'tempdb.. ##temp_result') and type='U')
BEGIN
    drop table ##temp_result
END

Score

Number of participants1MB+1 contribute+1 Collapse reason
admin + 1 + 1 Enthusiastic about helping others!

See all ratings

 Landlord| Posted on 12/6/2018 2:11:48 PM |
xzxmustwin Posted on 2018-12-6 11:58
To be honest, the structure of this table is a bit unreasonable

The homework questions assigned by the emmmm database teacher may indeed be unreasonable, and I didn't see it if I didn't learn it very well...
However, the key is still the problem-solving ideas, I want to ask my friends if you have any suggestions?
 Landlord| Posted on 12/6/2018 2:09:20 PM |
lzzsf posted on 2018-12-6 09:07
mysql is outdated, no client, give me the script for sql server

Sorry, I haven't used SQL Sever yet, maybe because it's just teaching, so the teacher asked us to use mysql to do it. If your friends are willing to share their ideas, thank you very much.
Posted on 12/5/2018 9:57:13 PM |
I'll go to the QQ group to offer a reward of 10 yuan to see if someone can help you answer no
Posted on 12/6/2018 7:56:39 AM |
Help
Posted on 12/6/2018 9:07:08 AM |
mysql is outdated, no client, give me the script for sql server

Score

Number of participants1MB+1 contribute+1 Collapse reason
admin + 1 + 1 Enthusiastic!

See all ratings

Posted on 12/6/2018 11:24:25 AM |
lzzsf posted on 2018-12-6 09:07
mysql is outdated, no client, give me the script for sql server

It is recommended to talk about the idea and let the landlord use it as a reference, which should also be possible
Posted on 12/6/2018 11:58:37 AM |
To be honest, the structure of this table is a bit unreasonable

Score

Number of participants1MB+1 contribute+1 Collapse reason
admin + 1 + 1 Enthusiastic!

See all ratings

 Landlord| Posted on 12/6/2018 2:07:29 PM |
Published on 2018-12-5 21:57
I'll go to the QQ group to offer a reward of 10 yuan to see if someone can help you answer no

Thanks, if anyone is willing to answer, I'll send you the red envelope :)
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com