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,-24,GETDATE())   -- Last 24 months
              AND VISITTYPE.VisitType in ('Surgery', 'Home', 'RACF') -- Face to face Visit Types
       GROUP BY VISITS.INTERNALID HAVING COUNT(*) >= 2               -- Having 2 or more visits
)
ORDER BY SURNAME,FIRSTNAME

-- Search for active patients using the definition '2 Face to face visits within 24 months'
-- Change the HAVING COUNT(*) >= value to change the number of visits (default is 2). 