I'm trying to merge overlapping dates between Admit and discharge dates of patients. There are a few edge cases which I couldn't cover in the query.
Input
+----+------------+--------------+
| ID | Admit_Dt | Discharge_Dt |
+----+------------+--------------+
| 1 | 12/30/2020 | 07/14/2021 |
+----+------------+--------------+
| 1 | 01/02/2021 | 07/14/2021 |
+----+------------+--------------+
| 1 | 06/16/2021 | 07/14/2021 |
+----+------------+--------------+
| 2 | 03/04/2021 | 03/25/2021 |
+----+------------+--------------+
| 2 | 05/01/2021 | 05/10/2021 |
+----+------------+--------------+
| 3 | 06/01/2021 | 06/05/2021 |
+----+------------+--------------+
Expected Output
+----+------------+--------------+
| ID | Admit_dt | Discharge_dt |
+----+------------+--------------+
| 1 | 12/30/2020 | 07/14/2021 |
+----+------------+--------------+
| 2 | 03/04/2021 | 03/25/2021 |
+----+------------+--------------+
| 2 | 05/01/2021 | 05/10/2021 |
+----+------------+--------------+
| 3 | 06/01/2021 | 06/05/2021 |
+----+------------+--------------+
Query I used the logic that was here But this doesn't cover the edge case for ID 2 and 3. Also the subquery is slower when the data is huge. Is it possible to tackle this problem using LAG?
SELECT dr1.* FROM Member_Discharges dr1
INNER JOIN Member_Discharges dr2
ON dr2.ADMIT_DT> dr1.ADMIT_DT
and dr2.ADMIT_DT< dr1.DISCHARGE_DT