Hi again, I didn't quite have this right. I was calculating the Treatment Ends incorrectly. Here is sample raw data for a patient.
drop table if exists #temp
create table #temp
([PatientID] varchar(8),[DueDate] date,[Completed] int,[Missed] int)
insert into #temp
values
('PatientA','9/23/2021',0,1)
,('PatientA','9/24/2021',1,0)
,('PatientA','9/25/2021',1,0)
,('PatientA','9/26/2021',1,0)
,('PatientA','9/27/2021',1,0)
,('PatientA','9/28/2021',1,0)
,('PatientA','9/29/2021',1,0)
,('PatientA','9/30/2021',1,0)
,('PatientA','10/1/2021',1,0)
,('PatientA','10/2/2021',1,0)
,('PatientA','10/3/2021',1,0)
,('PatientA','10/4/2021',1,0)
,('PatientA','10/5/2021',1,0)
,('PatientA','10/6/2021',1,0)
,('PatientA','10/7/2021',1,0)
,('PatientA','10/8/2021',1,0)
,('PatientA','10/9/2021',0,1)
,('PatientA','10/10/2021',1,0)
,('PatientA','10/11/2021',0,1)
,('PatientA','10/12/2021',1,0)
,('PatientA','10/13/2021',0,1)
,('PatientA','10/14/2021',1,0)
,('PatientA','10/15/2021',1,0)
,('PatientA','10/16/2021',1,0)
,('PatientA','10/17/2021',1,0)
,('PatientA','10/18/2021',1,0)
,('PatientA','10/19/2021',0,1)
,('PatientA','10/20/2021',1,0)
,('PatientA','10/21/2021',1,0)
,('PatientA','10/22/2021',1,0)
,('PatientA','10/23/2021',1,0)
,('PatientA','10/24/2021',1,0)
,('PatientA','10/25/2021',1,0)
,('PatientA','10/26/2021',1,0)
,('PatientA','10/27/2021',0,1)
,('PatientA','10/28/2021',1,0)
,('PatientA','10/29/2021',1,0)
,('PatientA','10/30/2021',1,0)
,('PatientA','10/31/2021',1,0)
,('PatientA','11/1/2021',1,0)
,('PatientA','11/2/2021',1,0)
,('PatientA','11/3/2021',1,0)
,('PatientA','11/4/2021',1,0)
,('PatientA','11/5/2021',1,0)
,('PatientA','11/6/2021',0,1)
,('PatientA','11/7/2021',0,1)
,('PatientA','11/8/2021',0,1)
,('PatientA','11/9/2021',0,1)
,('PatientA','11/10/2021',0,1)
,('PatientA','11/11/2021',0,1)
,('PatientA','11/12/2021',0,1)
,('PatientA','11/13/2021',0,1)
,('PatientA','11/14/2021',0,1)
,('PatientA','11/15/2021',0,1)
,('PatientA','11/16/2021',0,1)
,('PatientA','11/17/2021',0,1)
,('PatientA','11/18/2021',0,1)
,('PatientA','11/19/2021',0,1)
,('PatientA','11/20/2021',0,1)
,('PatientA','11/21/2021',0,1)
,('PatientA','11/22/2021',0,1)
,('PatientA','11/23/2021',0,1)
,('PatientA','11/24/2021',0,1)
,('PatientA','11/25/2021',0,1)
,('PatientA','11/26/2021',0,1)
,('PatientA','11/27/2021',0,1)
,('PatientA','11/28/2021',0,1)
,('PatientA','11/29/2021',0,1)
,('PatientA','11/30/2021',0,1)
,('PatientA','1/27/2022',0,1)
,('PatientA','1/28/2022',0,1)
,('PatientA','1/29/2022',0,1)
,('PatientA','1/30/2022',1,0)
,('PatientA','1/31/2022',0,1)
,('PatientA','2/1/2022',0,1)
,('PatientA','2/2/2022',1,0)
,('PatientA','2/3/2022',0,1)
,('PatientA','2/4/2022',0,1)
,('PatientA','2/5/2022',0,1)
,('PatientA','2/6/2022',0,1)
,('PatientA','2/7/2022',0,1)
,('PatientA','2/8/2022',0,1)
,('PatientA','2/9/2022',0,1)
,('PatientA','2/10/2022',0,1)
,('PatientA','2/11/2022',0,1)
,('PatientA','2/12/2022',0,1)
,('PatientA','2/13/2022',1,0)
,('PatientA','2/14/2022',0,1)
,('PatientA','2/15/2022',0,1)
,('PatientA','2/16/2022',0,1)
,('PatientA','2/17/2022',0,1)
,('PatientA','2/19/2022',0,1)
,('PatientA','2/20/2022',0,1)
The logic is a treatment ends on the 5th consecutive missed day. We want to know the start and end dates for each treatment "island". I didn't include a sample patient that has an active streak going, but for them I would want null for their end date. If you need data for an example I can create one.
For this patient the output would be:
PatientID | Start | End |
PatientA | 9/24/2021 | 11/10/2021|
PatientA | 1/30/2022 | 2/7/2022|
PatientA | 2/13/2022 | 2/19/2022|
Can someone please try to come up with logic to help with this? I was trying by have a running total for the missed treatments, once that hit 5 it was an end. But I couldn't figure out how to determine the starts.
Thank you again! Seems like a fun logic puzzle but I'm just drawing blanks now.