Solved

Calculate the interval of days between a discharge date and a subsequent admission date for the same Medical Record #

Posted on 2016-07-25
3
69 Views
Last Modified: 2016-07-26
I have attached a list of admissions by sorted Medical Record and VisitId number (desc).  I am trying to calculate the interval of days between a patient's subsequent admission and their most recent discharge date.  If the patient was not discharged previously to the current admission, the interval is equal to zero.

Here is the code to generate the attached list:

select
 AD.UnitNumber as MedRec#, AD.AccountNumber, AD.VisitID,AD.AdmitDateTime, AD.DischargeDateTime, AD.PatientClass,RANK () over(partition by AD.UnitNumber order by AD.VisitID desc) as ARnk  
from
 livedb.dbo.AbstractData AD  
left
 outer join livedb.dbo.AbsDrgDiagnoses ADR  
on
 AD.VisitID=ADR.VisitID  
where
 AD.AdmitDateTime >='2015-12-01' and LEFT(AD.PatientClass,2)='IN' and ADR.DiagnosisSeqID=1  
order
 by AD.UnitNumber,AD.VisitID desc

What can I add to perform the caclualtion?

Thank you

Glen
Readmit_Sample.xlsx
0
Comment
Question by:GPSPOW
3 Comments
 
LVL 40

Accepted Solution

by:
Sharath earned 500 total points
ID: 41728431
check this
;with cte as (
select
 AD.UnitNumber as MedRec,
 AD.AccountNumber, 
 AD.VisitID,
 AD.AdmitDateTime, 
 AD.DischargeDateTime, 
 AD.PatientClass,
 RANK () over(partition by AD.UnitNumber order by AD.VisitID desc) as ARnk  
from
 livedb.dbo.AbstractData AD  
left
 outer join livedb.dbo.AbsDrgDiagnoses ADR  
on
 AD.VisitID=ADR.VisitID  
where
 AD.AdmitDateTime >='2015-12-01' and LEFT(AD.PatientClass,2)='IN' and ADR.DiagnosisSeqID=1)
select c1.*,coalesce(datediff(dd,c2.DischargeDateTime, c1.AdmitDateTime),0) Interval
  from cte c1
  left join cte c2 on c1.MedRec = c2.MedRec and c1.ARnk+1 = c2.ARnk 
order
 by c1.UnitNumber,c1.VisitID desc 

Open in new window

2
 
LVL 26

Expert Comment

by:Chris Luttrell
ID: 41728757
If you are on SQL Server 2012 or later this will work, you can leave off the COALESCE if you are ok with NULL instead of 0:
SELECT
 AD.UnitNumber as MedRec#, AD.AccountNumber, AD.VisitID, AD.AdmitDateTime, AD.DischargeDateTime, AD.PatientClass,RANK () over(partition by AD.UnitNumber order by AD.VisitID desc) as ARnk,
 COALESCE(DATEDIFF(DAY, LEAD(AD.DischargeDateTime) OVER (PARTITION BY AD.UnitNumber ORDER BY AD.VisitID DESC), AD.AdmitDateTime),0) AS Interval 
from
 dbo.AbstractData AD  
left
 outer join dbo.AbsDrgDiagnoses ADR  
ON
 AD.VisitID=ADR.VisitID  
where
 AD.AdmitDateTime >='2015-12-01' and LEFT(AD.PatientClass,2)='IN' and ADR.DiagnosisSeqID=1  
order
 by AD.UnitNumber,AD.VisitID desc 

Open in new window

Results look like this
Query Results
0
 

Author Closing Comment

by:GPSPOW
ID: 41729392
Thanks this worked.  I had to change C1.UnitNumber to C1.MedRec

Glen
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
I've encountered valid database schemas that do not have a primary key.  For example, I use LogParser from Microsoft to push IIS logs into a SQL database table for processing and analysis.  However, occasionally due to user error or a scheduled task…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question