Hello,
I am developing a small java web-based car-pool booking system app which interacts with an access database. I am trying to write 2 queries: The first which will specify whether a given car is available on a given date range e.g. from: 1/12/05 to 12/12/05. the second which will run if the first query is unsuccessful e.g. a list of other cars available on the chosen dates. I have been looking at a Microsoft page which I believe may help do what I require but there are 2 separate queries in their example. I would prefer to have one using either a sub query or join, as I don't know of a way of providing Parameters into the second query using SQL, and its quite messy as well! The 2 queries they have given are below (This is based on a hotel reservation system but functionality is the same)
Booking.Arrival is DateTime field
Booking.Checkou t is DateTime field
Labelled queryAvailableR ooms -
PARAMETERS [Please enter arrival date] DateTime, [Please enter checkout date] DateTime;
-
-
SELECT Booking.Room, Booking.Arrival, Booking.Checkout
-
-
FROM Booking
-
-
WHERE (((Booking.Arrival) Between [Please enter arrival date] And [Please enter checkout date]-1))
-
-
Or ((([Checkout]-1) Between [Please enter arrival date] And [Please enter checkout date]))
-
-
Or (((Booking.Arrival)<[Please enter arrival date]) And (([Checkout]-1)>[Please enter checkout date]-1));
-
The second query uses the first query (as Reserved Rooms) -
SELECT Rooms.RoomNum
-
FROM Rooms LEFT JOIN queryAvailableRooms ON Rooms.RoomNum=queryAvailableRooms.Room
-
WHERE (((queryAvailableRooms.Room) Is Null));
-
Ideally I want to know how these 2 queries can be joined into one so that a web form can pass the dates into it as a connection string.
Any help would be greatly appreciated.
Many Thanks
Mark
9 6016 NeoPa 32,566
Recognized Expert Moderator MVP
I hope this is what you mean... - SELECT BlahBlah,
-
FROM BlahBlah JOIN (other sub query) AS subQName ON BlahBlah.Fld1=subQName.Fld1
-
etc
Basically, you surround the SELECT subquery in parentheses () and name it (using AS name) then it becomes like any other data source.
A basic, but real life example would be : - SELECT subQuery.*
-
FROM (SELECT *
-
FROM [tblAccounts]) AS subQuery
This would be equivalent to - SELECT *
-
FROM [tblAccounts]
Using subqueries like this has the benefit that you have access to the whole of the SQL - therefore you have greater control.
Hi Thanks for your reply
I think I sort've already knew that part.
I tried the following code -
-
SELECT Rooms.RoomNum
-
FROM Rooms LEFT JOIN (
-
-
SELECT Booking.Room, Booking.Arrival, Booking.Checkout
-
-
FROM Booking
-
-
WHERE (((Booking.Arrival) Between [Please enter arrival date] And [Please enter checkout date]-1))
-
-
Or ((([Checkout]-1) Between [Please enter arrival date] And [Please enter checkout date]))
-
-
Or (((Booking.Arrival)<[Please enter arrival date]) And (([Checkout]-1)>[Please enter checkout date]-1))
-
-
)
-
-
AS queryAvailableRooms ON Rooms.RoomNum=queryAvailableRooms.Room
-
-
and access just moaned as it does about the expression being too difficult to work out. I've tried to space out the query so it looks ok. I've also left the parameters in for now.
Any extra help, always appreciated
mark
Hello again,
I think by a stroke of luck and some more playing, it was simple. -
-
SELECT Rooms.RoomNum
-
FROM Rooms LEFT JOIN (SELECT Booking.Room, Booking.Arrival, Booking.Checkout
-
-
FROM Booking
-
-
WHERE (((Booking.Arrival) Between #9/2/1999# And #9/6/1999#-1))
-
-
Or ((([Checkout]-1) Between #9/2/1999# And #9/6/1999#))
-
-
Or (((Booking.Arrival)<#9/2/1999#) And (([Checkout]-1)>#9/6/1999#-1))
-
-
) AS queryAvailableRooms ON Rooms.RoomNum = queryAvailableRooms.Room WHERE queryAvailableRooms.Room is null
-
-
-
I've done the above query and it seems to work. Ignore the specifics of the dates. They return the correct two rooms that they should! Now I have a framework, to add some car information.... ....
Thanks again, oh and how shall i close this post??!?!
tell a lie..........
I've amended my query so that it is like so: -
-
SELECT Cars.CarID
-
FROM Cars LEFT JOIN (SELECT CarBooking.CarID, CarBooking.datefrom,CarBooking.dateto
-
-
FROM CarBooking
-
-
WHERE (((CarBooking.datefrom) Between #12/3/2005# And #12/4/2005#))
-
-
Or ((([CarBooking.dateto]) Between #12/3/2005# And #12/4/2005#))
-
-
Or (((CarBooking.datefrom)<#12/3/2005#) And (([CarBooking.dateto])>#12/4/2005#))
-
-
) AS queryAvailableCars ON Cars.CarID = queryAvailableCars.CarID
-
WHERE queryAvailableCars.CarID is null;
-
-
I have the following booking table:
Booking ID, Car ID, date from, date to
1 , 1, 12/01/2005, 15/05/2005
2 , 2, 14/01/2005, 15/02/2005
3 , 3, 13/01/2005, 31/01/2006
I would assume that the only Car to be available is car 2, since its date range is not within the 2 specified in the query,
However the query returns 1 and 2.
do you have any ideas anyone?!?
MMcCarthy 14,534
Recognized Expert Moderator MVP
I'm pretty sure the problem lies in the bracketing. I've simplified the query down to the basis requirements. If you paste it into the sql query window in Access the Jet engine will impose further brackets but that's not a problem. Try it and see what result you get. -
-
SELECT Cars.CarID
-
FROM Cars LEFT JOIN (SELECT CarID, datefrom, dateto
-
FROM CarBooking
-
WHERE (datefrom Between #12/3/2005# And #12/4/2005#)
-
Or (dateto Between #12/3/2005# And #12/4/2005#)
-
Or (datefrom<#12/3/2005# And dateto>#12/4/2005#)) AS queryAvailableCars
-
ON Cars.CarID = queryAvailableCars.CarID
-
WHERE queryAvailableCars.CarID is null;
-
-
Alternatively you could try: -
-
SELECT CarID FROM Cars
-
WHERE CarID NOT IN (SELECT CarID FROM CarBooking
-
WHERE (datefrom Between #12/3/2005# And #12/4/2005#)
-
Or (dateto Between #12/3/2005# And #12/4/2005#)
-
Or (datefrom<#12/3/2005# And dateto>#12/4/2005#));
-
-
NeoPa 32,566
Recognized Expert Moderator MVP
Try :- - SELECT CarID
-
FROM Cars LEFT JOIN (SELECT CarID
-
FROM CarBooking
-
WHERE ((datefrom<[Please enter checkout date])
-
AND (dateto>[Please enter arrival date]))) AS subCarBooking
-
ON Cars.CarID=subCarBooking.CarID
-
WHERE subCarBooking.CarID IS NOT Null;
Hello guys,
The first two attempts at the sql posted by mmccarthy came up with the same issue. that is, Car 1 and car 2 were selected. using neo's code, came up with cars 1 and 3. note that only car two should appear. this got me thinking that the query need only be changed slightly. I tried this: -
-
SELECT Cars.CarID
-
FROM Cars LEFT JOIN (SELECT CarID
-
FROM CarBooking
-
WHERE ((datefrom<[Please enter checkout date])
-
AND (dateto>[Please enter arrival date]))) AS subCarBooking
-
ON Cars.CarID=subCarBooking.CarID
-
WHERE subCarBooking.CarID IS Null;
-
-
and this worked! I will have to try out other dates for testing and will let you know how it goes.
thanks guys, I'm not a complete noob to sql (having had 1 yrs experience) but this was largely in oracle 9. access doesn't like the same syntax ive found!
MMcCarthy 14,534
Recognized Expert Moderator MVP
Good catch on the logic:
This one should also work
SELECT CarID FROM Cars
WHERE CarID NOT IN (SELECT CarID FROM CarBooking
WHERE (((datefrom Between #12/3/2005# And #12/4/2005#)
And (dateto Between #12/3/2005# And #12/4/2005#))
Or ((datefrom<#12/3/2005# And dateto>=#12/3/2005#)
Or (datefrom<#12/4/2005# And dateto>=#12/4/2005#))));
NeoPa 32,566
Recognized Expert Moderator MVP
Thanks guys, I'm not a complete noob to SQL (having had 1 yrs experience) but this was largely in oracle 9. Access doesn't like the same syntax I've found!
No you're not.
I've just reviewed my SQL and noticed two mistakes - both of which you've fixed in your version.
The second mistake was particularly stupid, as it would give the equivalent of an INNER JOIN - Doh!
Well done.
If no-one has any objections, I'll mark this thread as solved.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Dave Robinson |
last post by:
I was wondering if anyone could help me with a problem I'm having.
I've been using Dreamweaver to create a hotel booking system for a
friend of mine, using MySQL (version 4.0.21) and PHP 5. The bit I'm
struggling with is checking the Room Availability based on dates that
are typed into a textfield and then returning a list of the available...
|
by: Thomas R. Hummel |
last post by:
Hello all,
I am trying to write a query that compares a member's enrollment period
with the products that their group has had during that period (all
members belong to a group and the products that the member has are
based on that group). I need to get the date range for all products
that the member had during their enrollment.
Here are...
|
by: Lapchien |
last post by:
My table has a record called date/time - filled (not surprisingly) with a
date and time. I can format the date in one query field, also the time,
using format in an expression. My user wants me to provide a listing of all
table entries between 22:00 and 07:30 the next day, between a given set of
dates (typically one week apart but in...
|
by: Dalan |
last post by:
I have attempted to resolve a problem regarding erroneous output using
a Between And parameter on several
Access 97 queries, but to no avail. The queries are used for report
output and regardless of the beginning and ending dates entered, the
reports show all activity including before and after the dates
entered.
There is a frmAccount...
|
by: Steve Elliott |
last post by:
I have a query set up to gather together data between two specified dates.
Shown in the query column as:
Between #24/09/2004# And #01/10/2004#
Is it possible to enter several different date ranges, ie between 24/09/2004
and 01/10/2004 together with 05/10/2004 and 07/10/2004 ?
If I enter the "Between" criteria on different lines it...
| |
by: PC Datasheet |
last post by:
Transaction data is given with date ranges:
Beginning End
4/1/06 4/4/06
4/7/06 4/11/06
4/14/06 4/17/06
4/18/06 4/21/06
426/06 4/30/06
I am looking for suggestions on how to find the date ranges where there were
no transactions.
|
by: Matt |
last post by:
Hi all,
I have a database with a table storing a list of names, invoice dates, and
invoice amounts.
What I'm looking to do is to create a sum of the invouice amounts based on a
range of invoice dates. e.g. Sum of invouice amounts for invoice date range
between date1 and date2 then sum of invoice amount for date range between 3
and 4.
|
by: boliches |
last post by:
I am trying to get a crosstab query (in access 2000) to group data by date range.
TRANSFORM Sum(tblInvoice.InvBalance) AS SumOfInvBalance
SELECT tblInvoice.DealerID, Sum(tblInvoice.InvBalance) AS
FROM tblInvoice
GROUP BY tblInvoice.DealerID
PIVOT Format(Now(),"ww");
I know the above code is wrong! My question is how do I get the query...
|
by: Alex30093 |
last post by:
OK This is my 1st post to any forum, but I rely on forums all the time. Short story is I find myself needing to become a Access DBA for work.
Situation: I want to use one table of events that Track (Employee ID, Employee Name, Event Date and Trip Hours) And be able to add the fields(Week #1, Week #2, Week#3, week #4 and Week #5). *****There is...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it. ...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| | |