Is there any way to write a query to return the available time gaps, I'm thinking here that we would need to generate a temporary table in the query and return the data from this table that doesn't join with data in the appointment table but I'm guessing and was wondering whether there's an easier way..
The table definition is
CREATE TABLE [dbo].[SALON_Appointment] (
[AppointmentID] [int] IDENTITY (1, 1) NOT NULL ,
[Subject] [nvarchar] (64) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[Notes] [nvarchar] (64) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[StartTime] [datetime] NOT NULL ,
[EndTime] [datetime] NOT NULL ,
[StylistID] [int] NOT NULL ,
[Recurrency] [nvarchar] (100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[FirstStartTime] [datetime] NOT NULL ,
[FirstEndTime] [datetime] NOT NULL ,
[UserID] [int] NOT NULL
) ON [PRIMARY]
GO
Thanks in advance
Hi
You don't say what version of SQL Server you are using and there are a number of pieces of information/constraints missing.
These question include:
What is the length of the working day - does midnight to 07:00 counts as free time? Does the system need to allow for breaks such as lunch time? You could book them as dummy appointments (probably need an appointment type field to allow them to be stripped out under certain circumstances). What is the minimum length for an appointment - is 09:30 to 09:35 a valid gap? What days do you need this for: All Days; Working Days; Days for which appointments exist.|||I assume that you wana find the gap based on stylistID.
Code Snippet
SELECT A1.StylistID,
DATEDIFF(mi,A1.EndTime, A2.StartTime) AS Gap
FROM Appointment A1 JOIN Appointment A2 ON (A1.StylistID=A2.StylistID)
WHERE (A1.EndTime=
(SELECT MAX(A3.EndTime)
FROM Appointment A3
WHERE (A3.StylistID = A1.StylistID)
AND (A3.EndTime <= A2.StartTime)))
Hope this helps,
Paraclete
|||Hi,Thanks for the reply.
I think I've worked a solution out which is similar to your method. I create a temporary and fill it with psuedo appointments, I then do a select which uses a sub query with NOT EXISTS to select items from the pseudo table that don't exist in the real appointment table and hence returns a list of available slots.
It looks like a little less code than your method but I've not tested it fully so I'm not sure whether its 100% yet.
No comments:
Post a Comment