gpt4 book ai didi

mysql - 统计上个月 SQL 的记录数

转载 作者:行者123 更新时间:2023-11-29 06:33:29 25 4
gpt4 key购买 nike

你能帮我完成一个简单的任务,但我真的不知道我应该如何完成这个请求。请帮助。

我有:

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

我需要显示上个月为最多患者提供服务的医生。我无法计算出由医生服务的病人数量。您能建议一下这个请求吗?

最佳答案

您需要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到你喜欢的。

关于mysql - 统计上个月 SQL 的记录数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55076513/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com