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:
AD.UnitNumber as MedRec#, AD.AccountNumber, AD.VisitID,AD.AdmitDateTim
e, AD.DischargeDateTime, AD.PatientClass,RANK () over(partition by AD.UnitNumber order by AD.VisitID desc) as ARnk
outer join livedb.dbo.AbsDrgDiagnoses
AD.AdmitDateTime >='2015-12-01' and LEFT(AD.PatientClass,2)='I
N' and ADR.DiagnosisSeqID=1
by AD.UnitNumber,AD.VisitID desc
What can I add to perform the caclualtion?