I have a T-SQL query that’s giving me the most recent office visit in 2010 that wasn’t a dental visit. The relevant part of the query is:
AND pv.PatientVisitId IN (
SELECT Max(pv1.PatientVisitID)
FROM PatientVisit pv1
JOIN DoctorFacility df1 ON pv1.FacilityID = df1.DoctorFacilityID
JOIN PatientVisitResource pvr1 ON pv1.PatientVisitId = pvr1.PatientVisitId
JOIN DoctorFacility dfr ON pvr1.ResourceId = dfr.DoctorFacilityId
WHERE pv1.PatientProfileID = pp.PatientProfileID
AND pv1.Visit < '2011-01-01'
AND df1.ListName NOT LIKE '%Dental%'
)
Now, I want to flip that around to get the most recent office visit for the patients who only had dental appointments. I keep hitting the wall here, though. Can anyone bust me through to the other side? 🙂
The clue is to use where not exists