Home > other >  MySQL, query for finding patient readmissions, 30 days after discharge date
MySQL, query for finding patient readmissions, 30 days after discharge date

Time:09-07

I have a table that has columns visit_no, mr_no, admit_date, admit_time, dischg_date, dischg_time, and other columns that aren't necessary for the query. mr_no is the main identifier of the patient, a patient can have multiple visit_no with their respective admit / discharge dates. I almost found a solution but the query I have so far is inconsistent and giving me multiple visit_no for some entries when there should only be one. Here is an example of the table for a specific patient using their mr_no to query, the table name is chmadmtr:

Query:

SELECT 
    visit_no, mr_no, admit_date, 
    dischg_date, 
FROM
    chmadmtr
WHERE
    mr_no = '508'
ORDER BY admit_date ASC
visit_no mr_no admit_date dischg_date
1 508 8/15/2019 8/20/2019
2 508 8/27/2019 9/3/2019
3 508 9/13/2019 9/25/2019
4 508 9/29/2019 10/7/2019
5 508 10/19/2019 10/23/2019
6 508 11/13/2019 11/25/2019
7 508 12/13/2019 12/23/2019
8 508 1/7/2020 1/16/2020
9 508 2/1/2020 2/6/2020
10 508 2/10/2020 2/14/2020

Now what I would like is a query that checks the previous visit_no, and checks to see if they have been admitted before 30 days of their previous discharge. This query I have adds two columns at the end, one named 'Readmit' that will be either 'Y' (yes, they have been readmitted 30 days before previous discharge), or 'N' (No, they have not been readmitted 30 days before the previous discharge); and a column 'Days_between', that will give a count of how many days in between the admission and previous discharge, if it is a readmission before 30 days.

Here is the query:

SELECT 
    chmadmtr.visit_no, chmadmtr.mr_no, chmadmtr.admit_date,
    chmadmtr.dischg_date,
    IF(DATEDIFF(chmadmtr.admit_date, previ.dischg_date) < 30, 'Y', 'N') 
    Readmit,
    DATEDIFF(chmadmtr.admit_date, previ.dischg_date) Days_Between

FROM chmadmtr 
LEFT JOIN chmadmtr previ  ON chmadmtr.mr_no = previ.mr_no
    AND DATEDIFF(chmadmtr.admit_date, previ.dischg_date) BETWEEN 1 AND 29
WHERE chmadmtr.mr_no = '508'
ORDER BY admit_date ASC

and Output:

visit_no mr_no admit_date dischg_date Readmit Days_between
1 508 8/15/2019 8/20/2019 N (NULL)
2 508 8/27/2019 9/3/2019 Y 7
3 508 9/13/2019 9/25/2019 Y 24
3 508 9/13/2019 9/25/2019 Y 10
4 508 09/29/2019 10/07/2019 Y 26
4 508 09/29/2019 10/07/2019 Y 4
5 508 10/19/2019 10/23/2019 Y 24
5 508 10/19/2019 10/23/2019 Y 12
6 508 11/13/2019 11/25/2019 Y 21
7 508 12/13/2019 12/23/2019 Y 18
8 508 01/07/2020 01/16/2020 Y 15
9 508 02/01/2020 02/06/2020 Y 16
10 508 02/10/2020 02/14/2020 Y 25
10 508 02/10/2020 02/14/2020 Y 4

For some reason I'm getting some duplicate visit_no columns, the original query has 47 records so this query should also have the same amount, but the second query gives me 73 records. Some visit_no even repeat 3 times. I notice it's always the last of the duplicate columns that is correct with the 'Days_between' column, comparing it to the previous admission. I don't understand why I'm getting duplicate columns if I'm Left Joining the table to itself using an alias. Any insights would be appreciated, maybe a different method might be better.

CodePudding user response:

I took into account you would need to do it with more than one patient at a time.

select *
      ,case when datediff(admit_date,lag(dischg_date) over(partition by mr_no order by dischg_date))  <= 30 then 'Y' else 'N' end  as Readmit
      ,datediff(admit_date,lag(dischg_date) over(partition by mr_no order by dischg_date)) as Days_between 
from t
visit_no mr_no admit_date dischg_date Readmit Days_between
1 508 2019-08-15 2019-08-20 N
2 508 2019-08-27 2019-09-03 Y 7
3 508 2019-09-13 2019-09-25 Y 10
4 508 2019-09-29 2019-10-07 Y 4
5 508 2019-10-19 2019-10-23 Y 12
6 508 2019-11-13 2019-11-25 Y 21
7 508 2019-12-13 2019-12-23 Y 18
8 508 2020-01-07 2020-01-16 Y 15
9 508 2020-02-01 2020-02-06 Y 16
10 508 2020-02-10 2020-02-14 Y 4

Fiddle

  • Related