I am trying to make an appointment to return all the municipalities and all the specialties .... being that even in that municipality does not have any provider for such specialty it should be listed with value 0 or null .... at the moment I am with this Query, I need some help.
select
cid.txt_cidade, esp.txt_especialidade, count(*) as QTD
from
tb_associadoespecialidade as assesp
left join
tb_especialidade as esp on esp.id_especialidade = assesp.id_especialidade
left join
tb_associado as ass on ass.id_associado = assesp.id_associado
left join
tb_cidade as cid on cid.id_cidade = ass.id_cidade
where
ass.id_categoria = 1 and txt_cidade like 'Tupã'
group by
cid.txt_cidade, esp.txt_especialidade
order by
cid.txt_cidade desc;
Diagram
Result expectancy
TXT_CIDADE ------ TXT_ESPECIALIDADE --------- count QTD
SP -------------- ESPEC01 ------------------- 10
SP -------------- ESPEC02 ------------------- 5
SP -------------- ESPEC03 ------------------- 15
RJ -------------- ESPEC01 ------------------- NULL
RJ -------------- ESPEC02 ------------------- 5
RJ -------------- ESPEC03 ------------------- NULL