SELECT *
FROM BPS_Patients
WHERE StatusText = 'Active'
AND EXISTS (
       SELECT VISITS.INTERNALID , COUNT(*)
       FROM VISITS
              INNER JOIN VISITTYPE ON VISITTYPE.VISITCODE = VISITS.VISITCODE
       WHERE VISITS.RECORDSTATUS = 1
              AND VISITS.INTERNALID = BPS_Patients.InternalID
              AND VISITS.VISITDATE >= DATEADD(MONTH,-1,GETDATE()) -- Last 1 months. Change the number here. 
              AND VISITTYPE.VisitType in ('RACF')                 -- Residential Aged Care Facilities (RACF)
       GROUP BY VISITS.INTERNALID HAVING COUNT(*) >= 1            -- Having 1 or more visits
)
ORDER BY SURNAME,FIRSTNAME

-- Search for Patients with a Nursing Home Visit in the last x months