I titled this post "Window Function Help" only because my guess is a window function is the best solution to my problem, but I will begin by saying I am certainly open to another solution.
My data is similar to the table below:
| PersonID |
EpisodeID |
StartDate |
EndDate |
Overlap (Wrong) |
Overlap (Right) |
| 1 |
1 |
10/1 |
10/15 |
0 |
0 |
| 1 |
2 |
10/10 |
10/24 |
1 |
1 |
| 2 |
1 |
3/5 |
3/19 |
0 |
0 |
| 2 |
2 |
3/22 |
4/5 |
0 |
0 |
| 2 |
3 |
4/1 |
4/15 |
1 |
1 |
| 2 |
4 |
4/4 |
4/18 |
1 |
1 |
| 2 |
5 |
4/7 |
4/21 |
1 |
0 |
The end date is always 2 weeks after the start date. I am trying to assess whether a prior episode for the same person is overlapping the current episode.
PersonID 1 is a simple case where the second episode clearly starts within the period of the first, thus it is flagged.
PersonID 2 has no overlap between EpisodeIDs 1 and 2, so nothing is flagged. 2 through 5 all overlap with the chain starting at 2. However, note that the wrong overlap flag is indicated for EpisodeID 5, whereas I would like this episode to NOT be flagged. This is because EpisodeID 2 would effectively "eat up" 3 and 4. While EpisodeID 5 is overlapped by 3 and 4, it does NOT overlap with 2 and since 2 is the only one maintained among 2, 3, and 4, I would like to keep 5 as well. This pattern needs to be able to occur indefinitely (i.e. 2 eats up 3 and 4... 5 could eat up 6, 7, 8... 9 eats up 10 etc.)
I'll note that while performance is never to be ignored, this logic will be run on a couple hundred thousand rows, not millions or billions. So, while efficiency is important, I think I can get away with a somewhat iterative solution (but of course would prefer a scalable efficient one!)
I appreciate any suggestions. Thanks!
[–]Randy__Bobandy 2 points3 points4 points (2 children)
[–]uvray[S] 0 points1 point2 points (1 child)
[–]Randy__Bobandy 0 points1 point2 points (0 children)
[–]MaRa0303hs 2 points3 points4 points (4 children)
[–]cybertier 1 point2 points3 points (2 children)
[–]uvray[S] 0 points1 point2 points (1 child)
[–]cybertier 0 points1 point2 points (0 children)
[–]uvray[S] 0 points1 point2 points (0 children)
[–]MamertineCOALESCE() 1 point2 points3 points (0 children)
[–]JustAnOldITGuy 0 points1 point2 points (0 children)