How can I find the latest completed date that is before a date in SQL Server

92 Views Asked by At

Using SQL Server, I am trying to get the latest date a client completed an assessment that is less than the current date (or census date in this case). This query should roll through every day of the client census, and if the client is on the census that day, their most recently completed assessment ID, the completed date, and the census date should be displayed.

I am currently trying the query below, but am still getting every assessment completed date for every day the client is on the census.

SELECT   AssessmentID
        ,Max(CompletedDate) compdt
        ,cn.CensusDate

FROM    [view_ods_as_assessment] ast
        JOIN [dbo].[view_ods_facility_patient] fp on ast.ResidentID = fp.PatientID and ast.FacilityID = fp.FacilityID
        JOIN [dbo].[view_ods_daily_census] cn on fp.PatientMRN = cn.PatientMRN and fp.FacilityID = cn.FacilityID
        --JOIN [dbo].[view_ods_payer] py on cn.PayerID = py.PayerID

WHERE   CensusDate between '11/01/2021' and '11/15/2021'

GROUP BY AssessmentID
        ,cn.CensusDate

HAVING  MAX(ast.CompletedDate) < cn.CensusDate

ORDER BY CensusDate, compdt

UPDATE: I have since changed my query, which now works but is very slow (>10 minutes).

SELECT   t1.FacilityID
        ,t1.ResidentID
        ,fp.PatientMRN
        ,fp.LastName
        ,fp.FirstName
        ,cast(t1.AssessDate as date) AssessDate
        ,t1.CmiState
        ,cast(t2.MxDt as date) CompletedDate
        ,cast(t2.CensusDate as date) CensusDate

FROM [view_ods_as_assessment] t1
    INNER JOIN [dbo].[view_ods_facility_patient] fp on t1.ResidentID = fp.PatientID and t1.FacilityID = fp.FacilityID   
    INNER JOIN (
    SELECT ResidentID, ast.FacilityID, CensusDate, max(CompletedDate) AS MxDt 
    FROM [view_ods_as_assessment] ast
         JOIN [dbo].[view_ods_facility_patient] fp on ast.ResidentID = fp.PatientID and ast.FacilityID = fp.FacilityID
         JOIN [dbo].[view_ods_daily_census] cn on fp.PatientMRN = cn.PatientMRN and fp.FacilityID = cn.FacilityID
    WHERE   ast.FacilityID = '12'
        and IsDeleted = 'N'
        and CompletedDate IS NOT NULL
        and CmiState IS NOT NULL
        and StrikeoutFlag = 'N'
        and SubmissionReq = 'Y'
        and Status = 'Accepted'
        and CompletedDate < CensusDate
        and CensusDate between '12/01/2021' and '12/12/2021'
    GROUP BY ResidentID, ast.FacilityID, CensusDate
) t2 ON t1.ResidentID = t2.ResidentID AND t1.FacilityID = t2.FacilityID and t1.CompletedDate = t2.MxDt

WHERE   t1.FacilityID = '12'
        and IsDeleted = 'N'
        and CompletedDate IS NOT NULL
        and CmiState IS NOT NULL
        and StrikeoutFlag = 'N'
        and SubmissionReq = 'Y'
        and Status = 'Accepted'

Sample:

AssessmentID compdt
1 2/1/2021
2 3/1/2021
3 8/1/2021
4 12/1/2021

Desired:

AssessmentID compdt CensusDate
2 3/1/2021 6/1/2021
3 8/1/2021 11/1/2021
3 8/1/2021 12/1/2021
4 12/1/2021 12/2/2021
0

There are 0 best solutions below