在我的表格中,每位医生都有患者。我尝试使用大小写来完成此操作,但是它不起作用。
SELECT DoctorID
CASE
WHEN COUNT(DISTINCT PatientID) = 0 THEN 'There is no doctor who hasn't admitted any patients. '
ELSE 'There is no doctor who hasn't admitted any patients.
FROM Visits
GROUP BY DoctorID;
我认为您想要not exists
。如果医生没有看病人,那么我就不会期望那个医生出现在visits
表中。因此:
select d.*
from doctors d
where not exists (select 1 from visits v where v.doctorid = d.doctorid);
下面会工作吗?
SELECT DoctorID, count(DISTINCT PatientID)
FROM VISITS
GROUP BY DoctorID
HAVING COUNT(DISTINCT PatientID) = 0