I am fairly new to SQL and I am having trouble assigning points to patients based on their visit date. I think a Recursive CTE is the best way to achieve this but I cannot wrap my head around actually writing it.
Each OrganizationMrn should be assigned 1 point for each visit EXCEPT when there has been another visit within the past 3 days. If the patient has multiple visits in 3 days, one point should be assigned.
Any help modifying my query to a recursive cte that assigns points as described would be greatly appreciated.
Examples:
A patient has these 7 visits: 1/1, 1/2, 1/3 1/4, 1/5, 1/6, 1/11. This patient should be assigned 3 points: 1 point (1/1-1/4), 1 point (1/5-1/6), 1 point (1/11).
Query explanation:
Patient 25 should have 5 total points;
Date range 2015-10-02 - 2015-10-05 should be assigned 1 point;
Date range 2015-11-08 - 2015-11-09 should be assigned 1 point;
The other dates do not have another date within 3 days, they should be assigned 1 point.
WITH CTE AS
(
SELECT
ROW_NUMBER() OVER (PARTITION BY OrganizationMrn ORDER BY [Date]) AS ROWNUMBER, *
FROM #RC
)
SELECT *,
ISNULL(DATEDIFF(DY,(SELECT OTHER.[Date] FROM CTE OTHER WHERE OTHER.OrganizationMrn = CTE.OrganizationMrn AND OTHER.ROWNUMBER = CTE.ROWNUMBER - 1), CTE.[Date]),0) AS DaysFromLastVisit,
CASE WHEN ISNULL(DATEDIFF(DY,(SELECT OTHER.[Date] FROM CTE OTHER WHERE OTHER.OrganizationMrn = CTE.OrganizationMrn AND OTHER.ROWNUMBER = CTE.ROWNUMBER - 1), CTE.[Date]),0) > 3 THEN 1 END AS POINTS
FROM CTE
ORDER BY OrganizationMrn, [Date];
ROWNUMBER OrganizationMrn Date DaysFromLastVisit POINTS
1 25 2015-10-02 0 NULL
2 25 2015-10-03 1 NULL
3 25 2015-10-05 2 NULL
4 25 2015-11-08 34 1
5 25 2015-11-09 1 NULL
6 25 2016-03-04 116 1
7 25 2016-05-04 61 1
8 25 2016-05-10 6 1
This is how #RC is being populated:
SELECT I.OrganizationMrn, CAST(R.DTTM AS DATE) AS Date
INTO #RC
FROM Standard SD
INNER JOIN Iorg I ON I.Person = SD.Patient
INNER JOIN Result R ON I.Person = R.Patient
WHERE
R.Entry = 'note'
AND R.DTTM >= DATEADD(M,-12,GETDATE())
AND OrganizationMrn = '25'
ORDER BY I.OrganizationMrn;
OrganizationMrn Date
25 2015-10-02
25 2015-10-03
25 2015-10-05
25 2015-11-08
25 2015-11-09
25 2016-03-04
25 2016-05-04
25 2016-05-10
How can I modify this CASE statement to only assign points to one of of the 3 dates? It currently is assigning points to each day, 10/2, 10/3, 10/5.
CASE
WHEN ISNULL(DATEDIFF(DY,(SELECT OTHER.[Date] FROM CTE OTHER WHERE OTHER.OrganizationMrn = CTE.OrganizationMrn AND OTHER.ROWNUMBER = CTE.ROWNUMBER - 1), CTE.[Date]),0) <= 3
THEN 1 ELSE 0 END AS POINTS