I have a table of patients that were admitted and discharged from the hospital. I am trying to find any patients that have an admission within 30 days of their previous discharge date. So for example, I have the table:
|Name||Admission Date||Discharge Date|
Then I would want to have a table of:
I am unsure of how to check the Discharge Date in row 1 against the Admission Date in row 2. I am using Microsoft SQL Server.
You can use
select t.* from (select t.*, lag(discharge_date) over (partition by name order by admission_date) as prev_discharge_date from t ) t where prev_prev_discharge_date > dateadd(day, -30, admission_date)