您能帮我完成一个简单的任务,但我真的不知道该如何处理该请求。请帮助。
我有:
doctors
id name profession
1 James Harden dental
2 James Jones therapist
3 LeBron James cardiologist
4 Kobe Braynt surgeon
5 Sabrina Williams nurse
6 Tyler Okonma speech therapist
patients
id name diagnostic
1 Mo Bamba tooth pulling out
2 Kaney West astma
3 Post Malone heart attack
4 Denzel Curry headache
5 Nicola Jokic stomac-ache
6 Dwayne Wade AIDS
visits
doctorId patientId visitDate
1 1 2019-03-09
2 4 2019-03-01
2 5 2019-02-26
2 6 2019-02-05
3 3 2019-03-03
4 2 2019-03-07
我需要证明上个月服务最多患者的医生。我迷失在计算正在为医生服务的患者人数上。你能提出要求吗?
答案 0 :(得分:1)
尝试一下
select d.name, count( distinct v.patientid) noofvisits
from visits v inner join
doctors d on v.doctorid=d.doctorid
where v.visitdate < '1-mar-2019'
group by d.name
order by noofvisits desc
答案 1 :(得分:1)
您需要group by doctorid
visits
表并加入doctors
表:
select d.name, g.counter
from doctors d inner join (
select doctorid, count(distinct patientid) counter
from visits
where
year(visitdate) = year(current_date - interval 1 month)
and
month(visitdate) = month(current_date - interval 1 month)
group by doctorid
order by counter desc limit 3
) g on g.doctorid = d.id
order by g.counter desc, d.name
您可以将limit 3
更改为自己喜欢的内容。