I use ADO database in Delphi 2010 (TADOQuery).
The destination is to find the available rooms and show its room rate for a small INN.
t_room
coderoom as string
coderoomtype as string
t_typeroom
coderoomtype as string
nameroomtype as string
priceroomtype as number
t_trans
datetrans as date
codepoeple as string
coderoom as string
dateintrans as date -> date check in
dateouttrans as date -> date check out
Currently I use the query below to show the room prices.
SELECT
t_room.coderoom, t_room.coderoomtype, t_roomtype.coderoomtype,
t_roomtype.nameroomtype, t_roomtype.priceroomtype
FROM
t_room
INNER JOIN
t_roomtype ON t_room.coderoomtype = t_roomtype.coderoomtype
ORDER BY
t_room.coderoom ASC;
And manage to show : (in ADOQuery1 and DBGrid1 in Delphi 2010)
coderoom | nameroomtype | priceroomtype
----------------------------------------
101 | VIP | 20
102 | VIP | 20
103 | Standart | 10
104 | Standart | 10
105 | Standart | 10
106 | Standart | 10
What I want to do is how to show the coderoom s that has not been booked or has not been checkedin within t_trans ? (for specific dates)
Maybe something like below (using NOT IN
operator) :
SELECT
t_room.coderoom, t_room.coderoomtype, t_room.notesroom,
t_roomtype.coderoomtype, t_roomtype.nameroomtype, t_roomtype.priceroomtype
FROM
t_room
INNER JOIN
t_roomtype ON t_room.coderoomtype = t_roomtype.coderoomtype
WHERE
t_room.coderoom NOT IN (SELECT *
FROM t_trans
WHERE [current book/checkin/out date not between dateintrans and dateoutrans]
ORDER BY coderoom ASC)
ORDER BY
t_room.coderoom ASC;
The question is how to find available rooms that aren't booked between the t_trans.datein and t_trans.dateout ?
I add some files to make it easy to understand what I want to do at : http://sidhiciang.com/myfiles/TRIAL%20Available%20Rooms.rar
When I use the code below return error : $7701C41F - Exception class EOleException with message "You have writen a subquest that can return more than one field without using EXISTS reserved word in the main query's FROM clause. Revise the SELECT statement of the subquery to request only one field."
The code are :
AQRoomAvailable1.SQL.Text := 'SELECT t_room.coderoom, t_room.coderoomtype, t_room.notesroom, t_roomtype.coderoomtype, t_roomtype.nameroomtype, t_roomtype.priceroomtype ';
AQRoomAvailable1.SQL.Text := AQRoomAvailable1.SQL.Text + 'FROM t_room INNER JOIN t_roomtype ON t_room.coderoomtype = t_roomtype.coderoomtype WHERE t_room.coderoom ';
AQRoomAvailable1.SQL.Text := AQRoomAvailable1.SQL.Text + 'NOT IN (SELECT * FROM t_trans x WHERE x.coderoom = t_room.coderoom AND ( (x.dateintrans BETWEEN ' + DateToStr(dtpDateIn1.Date) + ' AND ' + DateToStr(dtpDateOut1.Date) + ' ) ';
AQRoomAvailable1.SQL.Text := AQRoomAvailable1.SQL.Text + 'OR (x.dateouttrans BETWEEN ' + DateToStr(dtpDateIn1.Date) + ' AND ' + DateToStr(dtpDateOut1.Date) + ' ) ';
AQRoomAvailable1.SQL.Text := AQRoomAvailable1.SQL.Text + 'OR (' + DateToStr(dtpDateIn1.Date) + ' BETWEEN x.dateintrans AND x.dateouttrans) ) )';
I already read links below and did not find the answer and gotten more confused....
check availability of a room with SQL
mysql hotel room availability
listing rooms available[hotel reservation]
query for available rooms in hotel reservation
Select available rooms
selecting room type on room availabilty subquery
Room Booking Query
Room booking sql query
SQL Scheduling - Select All Rooms Available for Given Date Range
SQL Inner-join with 3 tables?
How can I join multiple SQL tables using the IDs?
SQL Query NOT Between Two Dates