select patient_code, ddr, dpa, actual_delivery_date, if(termination_of_pregnancy_reason=1,'Miscarriage',if(termination_of_pregnancy_reason=2,'Death After Delivery','')) as termination_of_pregnancy_reason, office from tracking_motherbasicinfo left join tracking_children on tracking_motherbasicinfo.id_patient=tracking_children.id_patient_mother left join tracking_pregnancy on tracking_pregnancy.id_patient_mother=tracking_motherbasicinfo.id_patient left join patient on patient.id=tracking_motherbasicinfo.id_patient left join lookup_hospital on concat(lookup_hospital.city_code, '/', lookup_hospital.hospital_code) = left(patient_code,8) where tracking_motherbasicinfo.id_patient not in (select id_patient_mother from tracking_children where tracking_children.id_patient_child!=0 and tracking_children.id_patient_child!=tracking_children.id_patient_mother ) and patient_code is not null group by patient_code
Comments