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

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Join & Write a Comment

If you having speed problem in loading SQL Server Management Studio, try to uncheck these options in your internet browser (IE -> Internet Options / Advanced / Security):    . Check for publisher's certificate revocation    . Check for server ce…
Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

758 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

20 Experts available now in Live!

Get 1:1 Help Now