...

View Full Version : Help With Hotel Queries



smallville69
04-08-2011, 05:36 PM
Hello I have a database in Microsoft SQL Server with tables relevant to a reservation system for a hotel/villa and need help creating a few queries to ontain relavant data:

To be able to find out a list of guests checking out on a specific date, grouped by villa type and providing a total number for that day (i.e. a count).

For that query I think I'd have to use 2 relevant tables a guest reservation and reservation table:

1. create table guest_reservation(
confirm_no int,
agent_id int,
g_name varchar (30),
g_phone varchar (10));

alter table guest_reservation
alter column confirm_no int
not null;

alter table guest_reservation
alter column agent_id int
not null;

alter table guest_reservation
alter column g_name varchar(30)
not null;

alter table guest_reservation
alter column g_phone varchar(10)
not null;

alter table guest_reservation
add constraint pk_guest_reservation primary key (confirm_no, agent_id, g_name, g_phone);

alter table guest_reservation
add constraint fk1_guest_reservation foreign key (confirm_no) references reservation
ON DELETE CASCADE;

alter table guest_reservation
add constraint fk2_guest_reservation foreign key (agent_ID) references agent
ON DELETE CASCADE;

alter table guest_reservation
add constraint fk3_guest_reservation foreign key (g_name, g_phone) references guest
ON DELETE CASCADE;

2. create table reservation(
confirm_no int,
credit_card_no char (16),
res_checkin_date datetime,
res_checkout_date datetime,
default_villa_type char (1),
price_plan char (1));

alter table reservation
alter column confirmation_no int
not null;

alter table reservation
add constraint pk_reservation primary key (confirmation_no);

alter table reservation
add constraint fk1_reservation foreign key (default_villa_type) references price_plan;

alter table reservation

add constraint fk2_reservation foreign key (price_plan) references price_plan;

I thought using a query like this would help, but it didn't seem to:

SELECT g_name, villa_type, COUNT(*) FROM guest_reservation, reservation
WHERE guest_reservation.confirm_no = reservation.confirm_no AND res_checkout_date = ‘insert date for when you would want to check out here’ GROUP BY villa_type;

Ideas/help?

smallville69
04-08-2011, 06:13 PM
I think I figured out the 1st question...

Another query I wanted help on was that if a guest wanted a certain type of room then if that type of room would be available on the dates they wanted to stay on.

I used JUST the Reservation table but I'm not sure if that quite would do what I want, here's what I currently had:

Select villa_type from reservation
where res_check_in_date not between '2011-10-08' and '2011-10-09'
and res_check_out_date not between '2011-10-08' and '2011-10-09'

Old Pedant
04-08-2011, 11:41 PM
No, that doesn't work at all.

Joe is going to check in to Villa 17 on May 1st and check out on May 20th.

Now along comes Mary, who wants to check in on May 10th and check out on May 15th.

You would do a query such as:

SELECT * FROM reservation
WHERE check_in NOT BETWEEN 'May 10' AND 'May 15'
AND check_out NOT BETWEEN 'May 10' AND 'May 15'

And, for Villa 17, indeed you would find that the Villa *IS* available! Wouldn't you?

This is the classic "overlap" problem. There are 6 possible scenarios.

I and O will be the *existing* reservation. S and E will be the *proposed* reservation.

Here are the possible "time lines":

-----I----O----S----E---- No overlap, new res okay
-----I----S---O---E------ Overlap
-----I----S-E-----O------ Overlap
---S----I----E--O-------- Overlap
----S---I---O---E-------- Overlap
----S---E---I---O-------- No overlap, new res okay

So it's actually easy to see the okay conditions:
-- When the Existing reservation ends before the New one starts
---When the Existign reservation starts After the New one ends

So, in SQL:


SELECT * FROM reservation
WHERE check_out <= new_checkin
OR check_in >= new_checkout

*BUT*...

But that only works if you are checking *ONE* reservation.

To check if a given room is available for a given period of time, you have to do *NEGATIVE* logic:

SELECT DISTINCT roomNumber FROM reservations
WHERE roomNumber NOT IN (
SELECT roomNumber FROM reservations
WHERE check_out >= new_checkin
AND check_in <= new_checkout )

In other words, the INNER select there finds all rooms where there *IS* an overlap.

And then the OUTER select says "give me all rooms where no overlap exists".

Make sense?

smallville69
04-09-2011, 10:23 PM
That does make sense Old Pendant, thanks!

I'm having trouble getting my queries to return appropriate results for the following queries though:

1. For a given specific date list the guests that are scheduled to check out, grouped by the villa type; additionally also present their count.

The guest reservation table has the following columns with data: (confirm_no, agent_id, g_name, g_phone)

The reservation table has the following columns with data: (confirm_no, credit_card_no, res_checkin_date, res_checkout_date, default_villa_type, price_plan).


SELECT g_name, default_villa_type
FROM guest_reservation, reservation
WHERE
guest_reservation.confirm_no = reservation.confirm_no
AND res_checkout_date = 24/12/2010
order by default_villa_type, g_name;


SELECT COUNT(*) as total_checking_out
FROM reservation
WHERE res_checkout_date = 24/12/2010

Shouldn't that query work? When I run it, I get NO errors simply no results returned (and the count is 0), and looking at my table I know there should be 2 guest names returning as they checkout that day.

Help?

smallville69
04-09-2011, 11:16 PM
Okay I was able to figure that out (it was because of the timestamp that was involved with the day I didn't realize).

The last query I need help with is for a specific reservation show the tentative cost the guest will have to pay.

Now this is a bit more complicated because there are two costs, one is the cost for the duration of their stay and the other is for another invoice they get billed to them from another invoice (which is for things such as dining during their stay).

The guest reservation table has the following columns with data: (confirm_no, agent_id, g_name, g_phone)

The reservation table has the following columns with data: (confirm_no, credit_card_no, res_checkin_date, res_checkout_date, default_villa_type, price_plan)

The invoice table has the following columns with data: (inv_no, inv_date, inv_amount, confirm_no).

The price plan table has the following columns with data: (price_plan, rate, default_villa_type, bed_type)

So I need to somehow list the guests name with their total amount due which will be the ((res_checkout_date-res_checkin_date) * rate) + inv_amount coming from the reservation table, price table and invoice table respectively (and the guest name from the guest reservation table which is linked through the confirm_no).

It seems complicated and I'm not even sure where to begin?

smallville69
04-10-2011, 12:38 AM
I tried both

select
g.g_name,
datediff(d, r.res_checkin_date, r.res_checkout_date)*p.rate+i.inv_amount
from reservation as r
inner join price_plan as p
on r.price_plan = p.price_plan
inner join invoice as i
on r.confirm_no = i.confirm_no
inner join guest_reservation as g
on r.confirm_no = g.confirm_no

and

SELECT guest_reservation.g_name, (DATEDIFF(d, reservation.res_checkout_date, reservation.res_checkin_date) * price_plan.rate ) + invoice.inv_amount
FROM guest_reservation JOIN invoice ON guest_reservation.confirm_no = invoice.confirm_no
JOIN reservation ON guest_reservation.confirm_no = reservation.confirm_no
JOIN price_plan ON reservation.price_plan = price_plan.price_plan

Both of those queries gave me the same result:

I had NO errors however NO results returned only a g_name column and a "no column name" column and no results in them?

smallville69
04-10-2011, 01:38 AM
So I figured out one query with the help here however I want to add one more thing into it now, so from before:

The guest reservation table has the following columns with data:

(confirm_no, agent_id, g_name, g_phone)

The reservation table has the following columns with data:

(confirm_no, credit_card_no, res_checkin_date, res_checkout_date, default_villa_type, price_plan)

The invoice table has the following columns with data:

(inv_no, inv_date, inv_amount, confirm_no).

The price plan table has the following columns with data:

(price_plan, rate, default_villa_type, bed_type)

So I need to somehow list the guests name with their total amount due which will be the ((res_checkout_date-res_checkin_date) * rate) + inv_amount coming from the reservation table, price table and invoice table respectively (and the guest name from the guest reservation table which is linked through the confirm_no).

To this I need to add what a guest may have ordered (food wise into their total)

So we have a dining_order table with the following columns with data:

(r_confirmation_no, item)

We have a dining_menu table with the following columns with data:

(item, price, description)

So with this query:

SELECT gr.g_name, (DATEDIFF(d, r.res_checkout_date, r.res_checkin_date) * pp.rate ) + ISNULL(i.inv_amount, 0) FROM guest_reservation gr LEFT OUTER JOIN invoice i ON gr.confirm_no = i.confirm_no JOIN reservation r ON gr.confirm_no = r.confirm_no JOIN price_plan pp ON r.price_plan = pp.price_plan;

I need to somehow add items that a guest has ordered from the dining_order table (which is linked with the r_confirm_no from the dining_order table equaling the confirm_no from the reservation table), the items price must be taken from the dining_menu table (where dining_order.item equals dining_menu.item) and added into the above query.

It can also be incorportated into this query which worked originally as well:

select g.g_name, datediff(d, r.res_checkin_date, r.res_checkout_date)*p.rate+coalesce(i.inv_amount, 0) as Amount from reservation as r
inner join priceplan as p on r.price_plan = p.price_plan inner join guest_reservation as g on r.confirm_no = g.confirm_no left outer join invoice as i on r.confirm_no = i.confirm_no



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum