Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
smallville69
Starting Member
10 Posts |
Posted - 2011-04-08 : 11:37:14
|
| 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_reservationalter column confirm_no intnot null;alter table guest_reservationalter column agent_id intnot null;alter table guest_reservationalter column g_name varchar(30)not null;alter table guest_reservationalter column g_phone varchar(10)not null;alter table guest_reservationadd constraint pk_guest_reservation primary key (confirm_no, agent_id, g_name, g_phone);alter table guest_reservationadd constraint fk1_guest_reservation foreign key (confirm_no) references reservationON DELETE CASCADE;alter table guest_reservationadd constraint fk2_guest_reservation foreign key (agent_ID) references agentON DELETE CASCADE;alter table guest_reservationadd constraint fk3_guest_reservation foreign key (g_name, g_phone) references guestON 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 reservationalter column confirmation_no intnot null;alter table reservationadd constraint pk_reservation primary key (confirmation_no);alter table reservationadd constraint fk1_reservation foreign key (default_villa_type) references price_plan;alter table reservationadd 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
Starting Member
10 Posts |
Posted - 2011-04-08 : 12:12:32
|
| 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 reservationwhere 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' |
 |
|
|
mmarovic
Aged Yak Warrior
518 Posts |
Posted - 2011-04-08 : 15:48:01
|
| Look at the exists operator in Books on Line.MirkoMy blog: http://mirko-marovic-eng.blogspot.com/ |
 |
|
|
smallville69
Starting Member
10 Posts |
Posted - 2011-04-09 : 16:25:45
|
| 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_typeFROM guest_reservation, reservationWHEREguest_reservation.confirm_no = reservation.confirm_noAND res_checkout_date = 24/12/2010order by default_villa_type, g_name;SELECT COUNT(*) as total_checking_outFROM reservationWHERE res_checkout_date = 24/12/2010Shouldn'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
Starting Member
10 Posts |
Posted - 2011-04-09 : 17:18:12
|
| 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
Starting Member
10 Posts |
Posted - 2011-04-09 : 18:37:49
|
| I tried bothselect g.g_name, datediff(d, r.res_checkin_date, r.res_checkout_date)*p.rate+i.inv_amountfrom 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_noandSELECT 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
Starting Member
10 Posts |
Posted - 2011-04-09 : 19:38:21
|
| 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 rinner 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 |
 |
|
|
|
|
|
|
|