Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Compare Patient visit times to previous appointment

$
0
0

Hello,

I have the following query, this query works well and it used to give me all appointments after the first appointment, i changed the following code from

min(AdmissionDateTime) over (partition by MRN, DX1) FirstVisitDate
(AdmissionDateTime) As VisitDate,DischargeDateTime,
obvious this is wrong.. as it calculates the diff between admission and Discharge.

what i am trying to change it to say

Patient readmitted within 30 days of the previous appointment DischargeDateTime

currently it is calculating the diff between DischargeDate and Visitdate.

I may need to go back to a query with a outer apply, but i was hoping to do it with a CTE

any help would be apprecitated

following

with visit as (
select 
InpatientEpisodeId,MRN,
 IndexAdmission,
(AdmissionDateTime) As VisitDate,DischargeDateTime,
row_number() over (partition by MRN, IndexAdmission order by AdmissionDateTime) visit_number,DRG,DRG_Description
from tbl.tbl)
select 
*,
case 
when visit_number = 1 then 'First Visit' 
else 
case when datediff(dd, DischargeDateTime,VisitDate ) < =30 then 'Within 30 Days'
else 'After 30 Days'
end 
end AS 'Within 30 Days'
from visit


David



Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>