简体   繁体   中英

SQL - How do I query for re-admissions in TSQL?

I'm trying to figure out how to query for readmissions on Server 2008r2. Here is the basic structure of the visit table. There are other fields but none that I thought would be helpful. One issue is that some of these may be transfers instead of discharges which I have no easy way to deduce but that issue can be ignored for now. I tried my hand at this but I guess my understanding of SQL needs more work. I tried to find any info I could online but none of the queries lead me to a useful conclusion or I just didn't understand. Any suggestions would be appreciated.

EDIT: Readmission is if a patient returns within 30 days of previous discharge.

+---------+--------+-----------------+-----------------+
| VisitID |  UID   |     AdmitDT     |   DischargeDT   |
+---------+--------+-----------------+-----------------+
|      12 | 2      | 6/17/2013 6:51  | 6/17/2013 6:51  |
|      16 | 3      | 6/19/2013 4:48  | 6/21/2013 13:35 |
|      18 | 3      | 6/11/2013 12:08 | 6/11/2013 12:08 |
|      21 | 3      | 6/12/2013 14:40 | 6/12/2013 14:40 |
|      22 | 3      | 6/13/2013 10:00 | 6/14/2013 12:00 |
|      25 | 2      | 6/11/2013 16:13 | 6/11/2013 16:13 |
|      30 | 1      | 6/20/2013 8:35  | 6/20/2013 8:35  |
|      31 | 7      | 6/13/2013 6:12  | 6/13/2013 6:12  |
|      34 | 3      | 6/12/2013 8:40  | NULL            |
|      35 | 1      | 6/12/2013 8:52  | NULL            |
|      38 | 2      | 6/12/2013 10:10 | 6/12/2013 10:10 |
+---------+--------+-----------------+-----------------+

Attempt at Code:

SELECT N2.*
FROM visitTable AS N1
INNER JOIN
visitTable AS N2 ON N1.UID = N2.UID
WHERE N1.EncounterID <> N2.EncounterID AND ( N2.AdmitDT BETWEEN N1.DischargeDT and DATEADD(DD,30, N1.DischargeDT))

try this: (Show me the visits where the admission date is after discharge for another earlier visit by the same patient)

Select * From visits v
Where Exists (Select * From Visits
              Where uid = v.uid 
                 and v.AdmitDT > DischargeDT)

You have not explained any business rules so I'll take a guess. A readmission is when multiple UID appear, and it is every record except the first one

Here is another method using windowing functions.

SELECT VT.*
FROM visitTable VT
INNER JOIN
(
SELECT VisitID, ROW_NUMBER() OVER (PARTITION BY UID ORDER BY AdmitDT) VisitCount
FROM visitTable
) RA
ON RA.VisitCount > 1 AND RA.VisitID = VT.VisitID

Here's a start:

sqlfiddle

new fiddle

It gets each visit for each UID in order of admitDT, then pairs each visit with the next visit in that result. If the current admit date is between the last discharge date and 30 days from then, select it. There are some weird points though - UID 1 is shown to have been admitted on 6/12/2012 and never discharged, but then admitted again on 6/20/2013 and discharged the same day.

edit: restructured a bit to reduce the number of joins

WITH cte AS (
  SELECT visitid,uid,dischargedt,admitdt,
    row_number()over(partition BY uid ORDER BY admitdt) AS r
  FROM t
  )
SELECT
c1.visitid AS v1, c2.visitid AS v2,
c1.uid,
c1.dischargedt as [Discharged from first visit],
c2.admitdt as [Admitted to next visit]
FROM cte c1
INNER JOIN cte c2 ON c1.uid=c2.uid
WHERE c1.visitid<>c2.visitid
AND c1.r+1=c2.r
AND c2.admitdt BETWEEN c1.dischargedt AND dateadd(d,30,c1.dischargedt )
ORDER BY c1.uid

Results :

| V1 | V2 | UID | DISCHARGED FROM FIRST VISIT |      ADMITTED TO NEXT VISIT |
|----|----|-----|-----------------------------|-----------------------------|
| 25 | 38 |   2 | June, 11 2013 16:13:00+0000 | June, 12 2013 10:10:00+0000 |
| 38 | 12 |   2 | June, 12 2013 10:10:00+0000 | June, 17 2013 06:51:00+0000 |
| 18 | 34 |   3 | June, 11 2013 12:08:00+0000 | June, 12 2013 08:40:00+0000 |
| 21 | 22 |   3 | June, 12 2013 14:40:00+0000 | June, 13 2013 10:00:00+0000 |
| 22 | 16 |   3 | June, 14 2013 12:00:00+0000 | June, 19 2013 04:48:00+0000 |

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM