如何选择哪个医生没有看病人?

问题描述 投票:0回答:2

在我的表格中,每位医生都有患者。我尝试使用大小写来完成此操作,但是它不起作用。

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; 
sql ssms
2个回答
1
投票

我认为您想要not exists。如果医生没有看病人,那么我就不会期望那个医生出现在visits表中。因此:

select d.*
from doctors d
where not exists (select 1 from visits v where v.doctorid = d.doctorid);

0
投票

下面会工作吗?

SELECT DoctorID, count(DISTINCT PatientID) 
FROM VISITS
GROUP BY DoctorID
HAVING COUNT(DISTINCT PatientID) = 0
© www.soinside.com 2019 - 2024. All rights reserved.