I have a table which holds hospital admission date, discharge date and the diagnosis:
Now I am trying to get count of readmission within last one year and the history for each encounter (Discharge date + Diagnosis). The outcome should looks something like this:
I am able to get the count but struggling with the history. This is my code:
SELECT
A.Encounter, A.Patient, A.AdmissionDt, A.DischargeDt,
(SELECT COUNT(*)
FROM MyTable B
WHERE A.Patient = B.Patient
AND B.AdmissionDt >= DATEADD(YY, -1, A.DischargeDt)
AND B.AdmissionDt < A.DischargeDt) AS Cnt
FROM
MyTable A
ORDER BY
AdmissionDt DESC
Can I get some help please?