set @start_date = '2017-09-01';
set @end_date = now();
select count(*) as total,
if(id_hospital=0, concat(city_code,'/',hospital_code),(select concat(city_code,'/',hospital_code) from lookup_hospital where lookup_hospital.id=id_hospital)) as hospital
from tracking_infant left join patient on patient.id=tracking_infant.id_patient
where timestampdiff(year,dob,@start_date)>=6 and timestampdiff(year,dob,@end_date)<=18
and
id_patient in
(select id_patient from (select id_patient from tracking_followup
union
select id_patient from club_patient
union
select id_patient from tracking_regime
)df
)
and linked_to_id_patient=0
and (is_dead=0 or death_date>@start_date)
and (is_abandoned=0 or abandoned_date>@start_date)
group by hospital
Comments