I have a query result like
|----------------------------------------------| |StaffId|BranchId|StartTime|EndTime |PatientId | |----------------------------------------------| |1 |1 |09:30:00 |09:35:00|Null | |1 |1 |09:35:00 |09:40:00|Null | |1 |1 |09:40:00 |09:55:00|1 | |1 |1 |09:55:00 |10:00:00|Null | |1 |1 |10:00:00 |10:05:00|Null | |1 |1 |10:05:00 |10:20:00|2 | |1 |1 |10:20:00 |10:25:00|NULL | |1 |1 |10:25:00 |10:40:00|3 | |1 |1 |10:40:00 |10:45:00|Null | |1 |1 |10:45:00 |10:50:00|Null | |1 |1 |10:50:00 |10:55:00|Null | |1 |1 |10:55:00 |11:00:00|Null | |----------------------------------------------|
but I want to be able to group all unused time intervals (those that do not have a patient ID), so the result
|----------------------------------------------| |StaffId|BranchId|StartTime|EndTime |PatientId | |----------------------------------------------| |1 |1 |09:30:00 |09:40:00|Null | |1 |1 |09:40:00 |09:55:00|1 | |1 |1 |09:55:00 |10:05:00|Null | |1 |1 |10:05:00 |10:20:00|2 | |1 |1 |10:20:00 |10:25:00|NULL | |1 |1 |10:25:00 |10:40:00|3 | |1 |1 |10:40:00 |11:00:00|Null | |----------------------------------------------|
Not quite sure how to do this, but any recommendations would be appreciated. Not sure if this is simpler, but query results were obtained by connecting the table at 5 minute intervals with patients using sql
INSERT INTO @Results(BranchId, StaffId, StartTime, EndTime) SELECT us.BranchId, us.StaffId, us.StartTime, us.EndTime FROM @UnusedSlots us left join @Results r on (NOT ((r.StartTime >= us.EndTime) OR (r.EndTime <= us.StartTime))) AND (us.StaffId = r.StaffId) where r.BranchId is Null
Where @UnusedSlots are available 5-minute slots and @Results contains reserved slots (with patient identifiers). If there is a way to build a merge into this statement, it will be much better.
I thought that I could use <T23> and Max(EndTime) , if I can highlight adjacent results without PatientId, but I'm not sure how to do this, I will end up with the one that merged with 09: from 30 until 11:00
source share