Попробуйте, объединяясь через Attend
и Appointment
.
SELECT
Patient.PID,
MAX(Appointment.AppDate) AS lastAttended
FROM
Patient
LEFT JOIN Attend ON Patient.PID = Attend.A_PID
JOIN Appointment ON Appointment.AppNumber = Attend.A_AppNumber
GROUP BY Patient.PID
Для пациентов, посещающих 2+ приема в месяц, вам нужно COUNT()
внутри HAVING
предложения.
SELECT
Patient.PID,
COUNT(Attend.A_AppNumber) AS numAppts
FROM
Patient
LEFT JOIN Attend ON Patient.PID = Attend.A_PID
GROUP BY Patient.PID
HAVING COUNT(Attend.A_AppNumber) >= 2