SELECT *
FROM BPS_Patients p
WHERE 1=1
AND StatusText = 'Active' --Active patients.
AND INTERNALID IN 
	(
	 SELECT InternalID 
	 FROM VISITREASON
	 WHERE 1=1
	 AND RECORDSTATUS = 1
	 AND REASON LIKE 'COVID-19 testing%' --Reason for Visit.
	 AND INTERNALID IN 
		( 
		 SELECT InternalID
		 FROM VISITS
		 WHERE 1=1
		 AND VISITCODE != 12
		 AND RECORDSTATUS = 1
		 GROUP BY InternalID
		 HAVING COUNT(*) <= 2 --2 Visits or less.
		)
	)
ORDER BY Surname, FirstName;
--All patients who have had 2 or less visits, of which all had a visit reason of: 'COVID-19 testing'.