Allocated Pen Query

thpus2

New member
Local time
Today, 11:41
Joined
May 14, 2004
Messages
6
I need to create a query which will show what pens on todays date do not have a bookings in them.
The two tables i would need to use are:
Booking
  1. Booking_number (which is the primary key)
  2. Pen_number (which is the foreign key)
  3. start_date
  4. end_date

Pen
  1. Pen_number (primary key)
  2. Block_number(which is the foreign key)

I know i need to use the date() function and is null but my query at present is in Sql
SELECT Pen.pen_no, Booking.start_date, Booking.end_date
FROM Pen LEFT JOIN Booking ON Pen.pen_no = Booking.pen_no
GROUP BY Pen.pen_no, Booking.start_date, Booking.end_date
HAVING (((Booking.start_date)<Date()) AND ((Booking.end_date)<Date())) OR (((Booking.start_date)>Date()) AND ((Booking.end_date)>Date())) OR (((Booking.start_date) Is Null) AND ((Booking.end_date) Is Null));

This works but it displays the same pen again if it has had more then one booking in it. How do i make so that i can produce a report that will have just the pens that are free once?
Thanx
Thpus
 
SELECT DISTINCT Pen.pen_no, Booking.start_date, Booking.end_date
FROM Pen LEFT JOIN Booking ON Pen.pen_no = Booking.pen_no
GROUP BY Pen.pen_no, Booking.start_date, Booking.end_date
HAVING (((Booking.start_date)<Date()) AND ((Booking.end_date)<Date())) OR (((Booking.start_date)>Date()) AND ((Booking.end_date)>Date())) OR (((Booking.start_date) Is Null) AND ((Booking.end_date) Is Null));
 
Even with SELECT DISTINCT the query displays all the times that pen has been booked in.
Very strange.
Thanx
Thpus
 
Are all the times different?
 
Some timespans overlap but most don't.
But the query repeats everyrecord that pen has been used.
So that i do get all the unallocated pens but like pen 4 repeats 4 times. So in report this does not look right.
Thanx
Thpus
 

Users who are viewing this thread

Back
Top Bottom