In the cohort exit section it talks about "magic". While the SQL's implementation may be magical and need not be further discussed, the exact logical algorithm that this query implements is not documented. This is quite important to understanding what it means to generate an exit cohort in OHDSI. The algorithm could be discussed informally, but it should express what considerations and/or cases are accounted for.
Consider refactoring this code so that previous queries populate a temporary cohort era table, with subject, begin/end. Then, a separate SQL fragment that might be more digestible could be written that collapses these eras. Doing both the era extraction and the collapse at the same time make the logic hard to follow. If these two operations were separated, perhaps both could be more easily explained.
In the cohort exit section it talks about "magic". While the SQL's implementation may be magical and need not be further discussed, the exact logical algorithm that this query implements is not documented. This is quite important to understanding what it means to generate an exit cohort in OHDSI. The algorithm could be discussed informally, but it should express what considerations and/or cases are accounted for.
Consider refactoring this code so that previous queries populate a temporary cohort era table, with subject, begin/end. Then, a separate SQL fragment that might be more digestible could be written that collapses these eras. Doing both the era extraction and the collapse at the same time make the logic hard to follow. If these two operations were separated, perhaps both could be more easily explained.