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
48 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

867 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now