SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 Database Design and Application Architecture
 Smile Select All Rooms Available for Given Date Ra
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

lacta
Starting Member

3 Posts

Posted - 09/08/2012 :  17:48:30  Show Profile  Reply with Quote

I am trying to built a hotel reservation system and I have the following tables:

hotel
id, etc
customer
id, etc

room
room_no, hotelid , type, facilities
101 | 1 etc 101 | 1 etc 101 | 1 etc

booking
id, room_no, customerid, datefrom , dateto
1| 101 | 2 | 03-03-2012 | 06-03-2012 1| 101 | 2 | 07-03-2012 | 12-03-2012

availability
room_no, date
101 | 03-03-2012
101 | 04-03-2012
101 | 05-03-2012
101 | 06-03-2012
101 | 03-04-2012
101 | 04-04-2012
101 | 05-04-2012
101 | 06-04-2012

at the availability table the owner of the hotel will store the dates that the room is available. as you see for example the room 101 is available for the dates 3-6/3/12 and 3-6/4/12. The booking table is the table tha stores the actual booking , when a room is booked it's dates will be deleted from the availability table. Now when a customer wants to search for a room available from 3/4 to 6/4/12 what query can i do to the availability table, to search all dates and see if there is a room for that date range?

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 09/09/2012 :  12:37:26  Show Profile  Reply with Quote
you need a calendar table or tally table for that


SELECT r.room_no
FROM dbo.CalendarTable('2012-04-03','2012-04-06',0,0)c
CROSS JOIN room r
LEFT JOIN availability a
ON a.[date] = c.[date]
AND a.room_no = r.room_no
GROUP BY r.room_no
HAVING SUM(CASE WHEN a.room_no IS NULL THEN 1 ELSE 0 END) =0


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

lacta
Starting Member

3 Posts

Posted - 09/09/2012 :  16:39:01  Show Profile  Reply with Quote

quote:
Originally posted by visakh16

you need a calendar table or tally table for that


SELECT r.room_no
FROM dbo.CalendarTable('2012-04-03','2012-04-06',0,0)c
CROSS JOIN room r
LEFT JOIN availability a
ON a.[date] = c.[date]
AND a.room_no = r.room_no
GROUP BY r.room_no
HAVING SUM(CASE WHEN a.room_no IS NULL THEN 1 ELSE 0 END) =0



thank you for the reply. can yoou explain me what the calendar table will do? i have one calendar_table where i insert all the dates of the current year and if it is weekday or holiday , does this do ?
do i need a room_no column there?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/



Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 09/09/2012 :  23:15:17  Show Profile  Reply with Quote
quote:
Originally posted by lacta


quote:
Originally posted by visakh16

you need a calendar table or tally table for that


SELECT r.room_no
FROM dbo.CalendarTable('2012-04-03','2012-04-06',0,0)c
CROSS JOIN room r
LEFT JOIN availability a
ON a.[date] = c.[date]
AND a.room_no = r.room_no
GROUP BY r.room_no
HAVING SUM(CASE WHEN a.room_no IS NULL THEN 1 ELSE 0 END) =0



thank you for the reply. can yoou explain me what the calendar table will do? i have one calendar_table where i insert all the dates of the current year and if it is weekday or holiday , does this do ?
do i need a room_no column there?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/






yep that would do

just replace dbo.Calendartable with your tablename

SELECT r.room_no
FROM YourCalendarTable c
CROSS JOIN room r
LEFT JOIN availability a
ON a.[date] = c.[date]
AND a.room_no = r.room_no
WHERE c.date >='2012-04-03' AND c.date<'2012-04-07'
GROUP BY r.room_no
HAVING SUM(CASE WHEN a.room_no IS NULL THEN 1 ELSE 0 END) =0


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

lacta
Starting Member

3 Posts

Posted - 09/11/2012 :  04:18:08  Show Profile  Reply with Quote
thank you . it worked !! perfect.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 09/11/2012 :  11:12:59  Show Profile  Reply with Quote
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.11 seconds. Powered By: Snitz Forums 2000