Start with a table of the time slots youhave each day.
CREATE TABLE TimeSlots
(timeslot TIMESTAMP NOT NULL PRIMARY KEY);
Put a lot of constraints on the appointments table.
CREATE TABLE Appointments
(client_id INTEGER NOT NULL,
start_time TIMESTAMP NOT NULL
CHECK (<< falls on 1/2 hr>>),
end_time TIMESTAMP NOT NULL,
CHECK (<< falls on 1/2 hr>>),
CHECK (INTERVAL (end_time - start_time) MINUTES
IN (INTERVAL 90 MINUTES,
INTERVAL 120 MINUTES)),
etc.);
The query will be something like this:
SELECT T.timeslot, COUNT(A.client_id) AS appt_tally
FROM Appointments AS A,
TImeSlots AS T
WHERE T.timeslot BETWEEN A.start_time AND A.end_time
GROUP BY T.timeslot;
HAVING COUNT(A.client_id) <= 4;
>> Stay informed about: Reservation System Database Design