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.

 All Forums
 Other Forums
 MS Access
 I cant figure out this query

Author  Topic 

SamHarnew
Starting Member

14 Posts

Posted - 2004-11-25 : 14:41:33
Ok, this might take awhile to explain but here goes, I have got a table of bookings (tblBookings) for a leisure centre, this contains the feilds fname, sname, sport, date, time, courtNo. I then have a table for each sport, tblBadmintonCouts, tblTennisCourts, tblTableTennis, tblSquash these each contain the fields court and area, court is the courtNo I mentioned earlier and area tells you weather the court is "indoors" or "outdoors". It is basically a list of all the courts for that sport. Then finally I have a table called tblTimes this has 1 field which is 'time' this has a list of all the times there are courts available, so for each of those time slots each court for each sport can be booked. rite, then I have got a form called frmBookings this has an input for sport which is a combo box from the tblSports, then there is an input box for dates, this is a combo box an that displays the next 14 days, then there is 2 time boxes, from ... to ... these are both combo boxes from the tblTimes ,I also have 2 input fields for f and s name, finally I have a list box, the thing I cant do is on the click of a button for the list box to display all the available courts, for each of these courts it has to show the court number, the time that court is available, and weather it is indoors or outdoors. Any help with this problem would be greatly appreciated as I have been trying to do this for a few days now and have got nowhere with my basic knowledge of sql. Many thanks Sam

P.S. if you can see a much better way of doing the following data structure please tell me!

o btw i am using access xp if it matters

rockmoose
SQL Natt Alfen

3279 Posts

Posted - 2004-11-25 : 14:52:10
Well, that was a lengthy eplanation.
Please post table structures and how the tables are related.
Having one table for each sport is probably complicating things for you.

rockmoose
Go to Top of Page

SamHarnew
Starting Member

14 Posts

Posted - 2004-11-25 : 15:04:11
sorry about this but i am a beginner to access, what do you mean by post table structure and how they are related? sorry im abit stupid
Go to Top of Page

rockmoose
SQL Natt Alfen

3279 Posts

Posted - 2004-11-25 : 15:19:35
Ok,
Basically what we want is:
The name of the table, the relevant columns, the pk of the table, how you relate the tables ( foreign keys )
eg.
tblTennisCourts( courtnbr int, size_sqr_ft number, primary key(courtnbr) )

If you can provide sample data that would be great as well
courtnbr size_sqr_feet
1 200
33 220

Provide the tecnical info that you think is needed for us to help you with your problem.

rockmoose
Go to Top of Page

SamHarnew
Starting Member

14 Posts

Posted - 2004-11-25 : 16:01:30
tblBookings( 'fName' text, 'sName' text, 'sport' text combobox(uses tblSports for its values), 'date' short date, 'time' short time combobox(lookup table is tblTimes), 'courtNo' int
i havent set a primary key on any feilds yet in any tables, im really sorry about wasting ur time on this beause i no vertully nothing!

fName ¦ lName ¦ sport ¦ date ¦ time ¦ courtNo
sam, harnew, badminton, 01/01/04, 12:00, 3
luke, corrigan, tennis, 02/01/04, 16:00, 5

tblTennisCourt( 'court' autonumber, 'area' text combobox(choices indoors and outdoors) its the same for all four of the sport court table

court ¦ area
1, indoors
2, indoors
3, indoors
4, indoors
5, Outdoors
6, Outdoors

tblTimes( ‘time’ short time)

time
8:00
9:00
10:00
11:00

20:00

does this help? thanks again for your time
Go to Top of Page

rockmoose
SQL Natt Alfen

3279 Posts

Posted - 2004-11-26 : 03:33:16
Hi,

You have to put primary keys on your tables.
That is very basic db design, and prevents duplicates to be entered in your tables.

One problem you have is to generate a list of available timeslots for a given date range,
The reason is that the database only stores factual data, i.e. the timeslots already booked.

You have a couple of options:
1. store all available timeslots in the database
2. store only booked timeslots in the database and dynamically display the available timeslots from the base tables

My example that follows uses option 2.
The problems with option 1, is that when a change in the database occurs ( say a court is added/removed )
then you have to add/remove all the timeslots already stored for that court.

Anyway, the example that follows can easily be modified to use Option 2;
just fill the tblBooking with all the possible timeslots that can be booked.

------------------------------------------------------------------------------------------

Example design that you might find easier to work with, than the current one.

tblSport( sport text, primary key(sport) )
sport
'Tennis'
'TableTennis'

tblCourt( courtnbr int, sport text, area text, primary key(courtnbr) )
-- you could use courtnbr+sport as primary key, depends on the requirements
courtnbr | sport |area
1, 'Tennis', 'indoor'
2, 'Tennis', 'outdoor'

-- put a foreign key between tblSport and tblCourt: tblCourt(sport) references tblSport(sport)

tblTime( time short time, primary key(time) )
time
8:00
9:00
10:00
11:00

20:00

tblDate( date time, leisure_center_open boolean, primary key(date) )
date | leisure_center_open
20041101,true
20041102,true
20041103,false
20041104,true
...
-- put as many dates as you want here

tblBooking( courtnbr int, date time, time short time, fName text, sName text, comment text, primary key(courtnbr,date,time) )
courtnbr | date | time | fName | sName | comment
1, 20041101, 08:00, 'John', 'Wayne', ''
1, 20041101, 09:00, 'John', 'Wayne', ''
1, 20041101, 10:00, 'Leisure Center', '', 'Closed for maintenance'
1, 20041101, 11:00, 'Leisure Center', '', 'Closed for maintenance'

-- put a foreign key between tblBooking and tblCourt: tblBooking(courtnbr) references tblCourt(courtnbr)
-- put a foreign key between tblBooking and tblDate: tblBooking(date) references tblDate(date)
-- put a foreign key between tblBooking and tblTime: tblBooking(time) references tblTime(time)


Comment:
"the thing I cant do is on the click of a button for the list box to display all the available courts,
for each of these courts it has to show the court number,
the time that court is available,
and weather it is indoors or outdoors"
-- Query 3 below will get you that.

-- modifiy the following so that it fits the sql dialect of MS access.

-- query 1 will give all available times for all courts
-- it basically returns all the combinations of courts+dates+times filtered on date range
select
tblCourt.courtnbr,
tblCourt.area,
tblDate.date,
tblTime.time
from
tblCourt
,tblDate
,tblTime
where
tblDate.leisure_center_open = 'true'
and tblDate.date between 'from_date' and 'to_date' -- here you have to specify the daterange

-- query 2 will give all booked times for all courts filtered on date range
select
tblCourt.courtnbr,
tblCourt.area,
tblBooking.date,
tblBooking.time,
tblBooking.fName,
tblBooking.sName
from
tblCourt
join tblBooking on tblCourt.courtnbr = tblBooking.courtnbr
where
tblBooking.date between 'from_date' and 'to_date' -- here you have to specify the daterange

-- Query 3
-- If we combine query1 + query2 then we can show all the available times
select
tblCourt.courtnbr,
tblCourt.area,
tblDate.date,
tblTime.time
from
tblCourt
,tblDate
,tblTime
left join tblBooking
on tblCourt.courtnbr = tblBooking.courtnbr
and tblBooking.date = tblDate.date
and tblBooking.time = tblTime.time
where
tblDate.leisure_center_open = 'true'
and tblDate.date between 'from_date' and 'to_date'
-- and tblBooking.courtnbr is null -- this filter will show only the timeslots that are available


rockmoose
Go to Top of Page

SamHarnew
Starting Member

14 Posts

Posted - 2004-11-27 : 05:53:02
if i put a primary key on date time and courtnbr wnt that not let me use the same courtnbr twice? and the same with date and time? thanks again for your help sam
Go to Top of Page

SamHarnew
Starting Member

14 Posts

Posted - 2004-11-27 : 05:57:18
actully i think i get you now, will it not let me make duplcates for when all the date time and sport match another date time and sport, how do you put a primary key on more than one column, thanks sam
Go to Top of Page

SamHarnew
Starting Member

14 Posts

Posted - 2004-11-27 : 05:59:37
iv figured it out now
Go to Top of Page

SamHarnew
Starting Member

14 Posts

Posted - 2004-11-27 : 07:57:41
i cant get that query working iv done everything else but am stuck on the last bit. it keeps on displaying the error 'join error' do you know what this could be? sam
Go to Top of Page

rockmoose
SQL Natt Alfen

3279 Posts

Posted - 2004-11-27 : 10:15:59
Please post the query that is not working.

rockmoose
Go to Top of Page

SamHarnew
Starting Member

14 Posts

Posted - 2004-11-27 : 12:02:21
ok, if i was to chage it slightly and say i only need to be able to show the courts for one time. i have manged to make two querys witch do this, but i cannot figure out how to combine the 2 into 1, here they are.

SELECT tblBooking.courtnbr, tblBooking.date, tblBooking.fName, tblBooking.sName, tblBooking.age, tblBooking.telephone, tblBooking.member, tblBooking.membershipNo
FROM tblBooking
WHERE (((tblBooking.date)=#1/1/2004#) AND ((tblBooking.time)=#12/30/1899 12:0:0#));

this just narrows it down to the right date and time

SELECT tblCourt.sport, tblCourt.area, tblCourt.courtnbr
FROM Query1 RIGHT JOIN tblCourt ON Query1.courtnbr=tblCourt.courtnbr
WHERE (((tblCourt.sport)="Badminton") AND ((Query1.courtnbr) Is Null));

this shows the avalible courts, and narrows down the sport, is it possible to combine the 2? thanks sam
Go to Top of Page

rockmoose
SQL Natt Alfen

3279 Posts

Posted - 2004-11-27 : 18:36:18
quote:
is it possible to combine the 2? thanks sam

What is the result you are expecting if You combine the two ?

Her is my analysis of the situation...

The queries you have provide work this way:
The first one shows a the bookings on all courts for a specific time.
The second one shows all the courts that are not booked for that specific time.

I think You are on the right track, but You have to think this way:
You are working with two sets of information;
(a) All the booked times.
(b) All the times possible to book.
Set (a) is stored in tblBooking
Set (b) is the combination of tblCourt + tblDate + tblTime ( all courts * all dates * all times )

What you want to show the user of the database is the following:
(c) All the times that are still available to book.
Set (c) = ( Set (a) - Set (b) )

Does this make any sense to You ?
If it does, work on the following:
1. try to get Set (a), which is: SELECT ... FROM tblBooking
2. try to get Set (b), which is: SELECT ... FROM tblCourt,tblDate,tblTime
3. try to get Set (c), which is: SELECT ... FROM tblBooking RIGHT JOIN tblCourt,tblDate,tblTime ON tblBooking.courtnbr = tblCourt.courtnbr AND tblBooking.date = tblDate.date AND tblBooking.time = tblTime.time WHERE tblBooking.xxx IS NULL

Am I on the right track here ?
If not, please post the current table stuctures, and the resultset you are trying to get.

rockmoose
Go to Top of Page

SamHarnew
Starting Member

14 Posts

Posted - 2004-12-04 : 06:10:06
i have done it now works great. thanks for all your taime and help.

sam
Go to Top of Page

rockmoose
SQL Natt Alfen

3279 Posts

Posted - 2004-12-04 : 06:48:38
You are welcome,
good that you were able to make it work

rockmoose
Go to Top of Page
   

- Advertisement -