Моя схема:

У меня есть:
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
7 John Snow pediatrician
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
7 Boo Aby headahce
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-02-13
4 2 2019-03-07
7 1 2019-02-14
7 7 2019-02-15
Мне нужно распечатать врачей, обслуживающих наибольшее число пациентов за предыдущий месяц.
Мой запрос:
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) -- return cur. year
and
month(visitdate) = month(current_date - interval 1 month) -- return prev. month
group by doctorid
order by counter
) g on g.doctorid = d.id
order by g.counter desc, d.name
Результаты моего запроса:
name counter
James Jones 2
John Snow 2
LeBron James 1 <- wrong
Мне нужно только вывестикак это:
name counter
James Jones 2
John Snow 2