Author |
Topic |
idragoev
Starting Member
1 Post |
Posted - 2008-02-27 : 05:23:21
|
Hi all,I have two tables - Planning and Appointments: Planning - contains a list of planned items. Used to define boundaries for a work day and defines based on type what can be done for each item.Id,TypeId - the type of the planned itemsBeginTime DateTime - begin date and time of the planned itemEndTime DateTime - end date and time for the planned itemIn the Planning table we can have as many records per day as we need:1, First Meeting, 1 Jan 2008 09:00, 1 Jan 2008 11:002, First Meeting, 1 Jan 2008 11:00, 1 Jan 2008 12:003, First Meeting, 1 Jan 2008 13:00, 1 Jan 2008 15:004, First Meeting, 1 Jan 2008 15:00, 1 Jan 2008 18:00Appointments - contanis a list with appointmentsId,BeginTime DateTimeEndTime DateTime1, 1 Jan 2008 09:00, 1 Jan 2008 09:302, 1 Jan 2008 10:00, 1 Jan 2008 11:003, 1 Jan 2008 11:00, 1 Jan 2008 11:304, 1 Jan 2008 14:00, 1 Jan 2008 15:30 What is needed?What I need is to a find a way to compare the planned items with the appointments and to return all the periods for which a planned time exists:Free planned time:1, 1 Jan 2008 09:30, 1 Jan 2008 10:002, 1 Jan 2008 11:30, 1 Jan 2008 12:00 3, 1 Jan 2008 13:00, 1 Jan 2008 14:004, 1 Jan 2008 15:30, 1 Jan 2008 18:00So, having two multitudes of periods,where the one specifies the planning templates and the other real used time, I need to find all the periods which can be used for another appointments.I've tried several aproaches, but I always faced performance problems. Thanks in advance. |
|
tprupsis
Yak Posting Veteran
88 Posts |
Posted - 2008-02-27 : 11:04:10
|
Try this. I assume no planning period spans across days and that there are no overlaps in the planning periods.DECLARE @Planning TABLE(Id int, TypeId varchar(50), BeginTime datetime, EndTime datetime)INSERT @PlanningSELECT 1, 'First Meeting', '1 Jan 2008 09:00', '1 Jan 2008 11:00' UNION ALL SELECT 2, 'First Meeting', '1 Jan 2008 11:00', '1 Jan 2008 12:00' UNION ALL SELECT 3, 'First Meeting', '1 Jan 2008 13:00', '1 Jan 2008 15:00' UNION ALL SELECT 4, 'First Meeting', '1 Jan 2008 15:00', '1 Jan 2008 18:00' UNION ALLSELECT 5, 'First Meeting', '2 Jan 2008 15:00', '2 Jan 2008 18:00' DECLARE @Appointments TABLE(Id int, BeginTime datetime, EndTime datetime)INSERT @AppointmentsSELECT 1, '1 Jan 2008 09:00', '1 Jan 2008 09:30' UNION ALLSELECT 2, '1 Jan 2008 10:00', '1 Jan 2008 11:00' UNION ALLSELECT 3, '1 Jan 2008 11:00', '1 Jan 2008 11:30' UNION ALLSELECT 4, '1 Jan 2008 14:00', '1 Jan 2008 15:30' UNION ALLSELECT 5, '2 Jan 2008 14:30', '2 Jan 2008 18:30';WITH FreeTime(Id, Type, BeginTime, EndTime) AS(SELECT P.Id, P.TypeId, ISNULL(CASE WHEN A.BeginTime > P.BeginTime THEN P.BeginTime ELSE A.EndTime END, P.BeginTime), ISNULL(CASE WHEN A.EndTime < P.EndTime THEN P.EndTime ELSE A.BeginTime END, P.EndTime)FROM @Planning P LEFT OUTER JOIN @Appointments A ON CONVERT(varchar,P.BeginTime,101) = CONVERT(varchar,A.BeginTime,101) AND A.BeginTime < P.EndTime AND A.EndTime > P.BeginTime)SELECT P.Id, P.TypeId, P.BeginTime, P.EndTime, CASE WHEN Max(F.BeginTime) > Min(F.EndTime) THEN NULL ELSE Max(F.BeginTime) END AS FreeTimeBegin,CASE WHEN Max(F.BeginTime) > Min(F.EndTime) THEN NULL ELSE Min(F.EndTime) END AS FreeTimeEndFROM @Planning P INNER JOIN FreeTime F ON P.Id = F.IdGROUP BY P.Id, P.TypeId, P.BeginTime, P.EndTime |
|
|
|
|
|