Dan Williams
3/16/2007 3:45:00 PM
I have the following SQL schema
Bookings
-------------
BookID
Hours
BookingStatus
---------------------
BookStatusID
BookingId
StatusID
UserId
Date
Statues
-----------
StatusID
Statuses included Created, Covered, Cancelled, Not Covered, To Be
Arranged, etc.
The BookingStatus table contains a history of the statuses a booking
goes through. Whenever a user updates the status of a booking, a new
record is inserted into the Booking Status with the corresponding
data.
I'm trying to create a report to show the number of bookings each user
has covered over a given time period. However, i only want to count
bookings that are still covered (i.e. have not been cancelled since
they were covered) and i don't want it to count bookings that have
been covered more than once. If it did, a user would be credited for
covering an individual booking more than once.
In other words, i need to fetch only the top 1 status of each booking
and count only those that are covered, if you see what i mean!
I could record the current status of the booking in my Bookings table,
but i'm worried about data integrity and keeping the booking status
consistant.
Anyone got any bright ideas about how i can either design my schema
better, or come up with an efficient SQL query to select my report? I
also need to sum the hours each user has covered, as well as a count
of bookings covered.
Many thanks for any suggestions
Dan