Question

I have two queries where the qryAvailability1 returns dates blocked for reservation while qryAvailability2 produces the totally available dates before any reservation take place.

I combine them in a final “without matching” query to define the available dates for reservation:

qryAvailability1:

SELECT tblReservations.PropertyID, tblDates.Date
FROM tblReservations, tblDates
WHERE (((tblDates.Date) Between [tblReservations]![CheckIn] And [tblReservations]![CheckOut]));

qryAvailability2:

SELECT tblProperties.PropertyID, tblDates.Date FROM tblProperties, tblDates;

The final “without matching” query:

SELECT qryAvailability2.PropertyID, qryAvailability2.Date
FROM qryAvailability2 LEFT JOIN qryAvailability1 ON (qryAvailability2.Date=qryAvailability1.Date) AND (qryAvailability2.PropertyID=qryAvailability1.PropertyID)
WHERE (((qryAvailability1.Date) Is Null))
ORDER BY qryAvailability2.PropertyID, qryAvailability2.Date;

Is there any way to have a single query statement into 1 query instead of three?

In other words, I need to replace the references to qryAvailability1 and qryAvailability2 with the sql statement which produce them (whatever I tried didn’t work at all).

Was it helpful?

Solution

Assuming your final query works (i haven't checked it), then to combine all three:

SELECT qryAvailability2.PropertyID, qryAvailability2.Date
FROM (
SELECT tblProperties.PropertyID, tblDates.Date FROM tblProperties, tblDates
) qryAvailability2 LEFT JOIN (
SELECT tblReservations.PropertyID, tblDates.Date
FROM tblReservations, tblDates
WHERE (((tblDates.Date) Between [tblReservations]![CheckIn] And [tblReservations]![CheckOut]))
) qryAvailability1 ON (qryAvailability2.Date=qryAvailability1.Date) AND (qryAvailability2.PropertyID=qryAvailability1.PropertyID)
WHERE (((qryAvailability1.Date) Is Null))
ORDER BY qryAvailability2.PropertyID, qryAvailability2.Date;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top