SELECT CONCAT(IF(pn.given_name IS NULL, '', CONCAT((pn.given_name), ' ')),
              IF(pn.middle_name IS NULL, '', CONCAT((pn.middle_name), ' ')),
              IF(pn.family_name IS NULL, '', CONCAT((pn.family_name), ' '))) AS 'Doctor Name',
       count(DISTINCT e.encounter_id)                                        as 'Total Patient'
FROM encounter e
         INNER JOIN encounter_provider ep on ep.encounter_id = e.encounter_id
         INNER JOIN provider p on p.provider_id = ep.provider_id
         INNER JOIN person_name pn on p.person_id = pn.person_id
WHERE e.encounter_type in
      (select encounter_type_id from encounter_type where name in ("EME", "Consultation", "OPD", "IPD"))
  and cast(e.encounter_datetime as date) BETWEEN '#startDate#' AND '#endDate#'
group by p.provider_id;
