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
77 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 41

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

Free Webinar: AWS Backup & DR

Join our upcoming webinar with experts from AWS, CloudBerry Lab, and the Town of Edgartown IT to discuss best practices for simplifying online backup management and cutting costs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Agent Timeout 5 69
How to order Highest and lowest value rows alternatively in SQL Server ? 4 63
Parse this column 6 34
Replication failure 1 19
There are some very powerful Data Management Views (DMV's) introduced with SQL 2005. The two in particular that we are going to discuss are sys.dm_db_index_usage_stats and sys.dm_db_index_operational_stats.   Recently, I was involved in a discu…
I am showing a way to read/import the excel data in table using SQL server 2005... Suppose there is an Excel file "Book1" at location "C:\temp" with column "First Name" and "Last Name". Now to import this Excel data into the table, we will use…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

740 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