Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
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
Medium Priority
93 Views
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
from
left
on
where
order

What can I add to perform the caclualtion?

Thank you

Glen
0
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

LVL 41

Accepted Solution

Sharath earned 2000 total points
ID: 41728431
check this
``````;with cte as (
select
from
left
on
where
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
``````
2

LVL 27

Expert Comment

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
from
left
ON
where
order
``````
Results look like this
0

Author Closing Comment

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

Glen
0

## Featured Post

Question has a verified solution.

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

by Mark Wills PIVOT is a great facility and solves many an EAV (Entity - Attribute - Value) type transformation where we need the information held as data within a column to become columns in their own right. Now, in some cases that is relatively…
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…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
###### Suggested Courses
Course of the Month5 days, 10 hours left to enroll