select concat(city_code,'/',hospital_code) as hospital_code, count(*) as total from (select tracking_motherbasicinfo.id_patient from tracking_motherbasicinfo left join tracking_pregnancy on tracking_pregnancy.id_patient_mother=tracking_motherbasicinfo.id_patient where tracking_motherbasicinfo.created_at between'2016-10-01' and '2017-09-30' and is_ptme =1 or (tracking_motherbasicinfo.created_at >= '2016-10-01' and ((dpa between'2016-10-01' and '2017-09-30') or (actual_delivery_date between'2016-10-01' and '2017-09-30'))) group by id_patient )a left join patient on patient.id=id_patient group by concat(city_code,'/',hospital_code)