select h.hoscode hospcode,cast(h.hosname as char(200)) hospname,count(*) target,sum(result="Y") result,sum(result="Y")/count(*)*100 percent
from chospital_amp h
left join (
select p.hospcode,p.cid,p.hn,p.pid,concat(p.name," ",p.lname) ptname
,l.bdate
,group_concat(a.ga order by a.date_serv) ga
,group_concat(distinct if(a.ga<=12,"Y",null)) result
from labor l
join person p on p.pid=l.pid and l.hospcode=p.hospcode
left join anc a on a.pid=l.pid and a.hospcode=l.hospcode and a.ancno=1
where l.bdate between "2015-04-01" and "2016-03-31"
and p.typearea in (1,3) and p.discharge = 9
group by p.hospcode,p.pid
) a on a.hospcode=h.hoscode
group by a.hospcode
UNION
select "รวมทั้งคัพ" as hoscode,"" as hosname
,count(*) target
,sum(result="Y") result
,sum(result="Y")/count(*)*100 percent
from chospital_amp h
left join (
select p.hospcode,p.cid,p.hn,p.pid,concat(p.name," ",p.lname) ptname
,l.bdate
,group_concat(a.ga order by a.date_serv) ga
,group_concat(distinct if(a.ga<=12,"Y",null)) result
from labor l
join person p on p.pid=l.pid and l.hospcode=p.hospcode
left join anc a on a.pid=l.pid and a.hospcode=l.hospcode and a.ancno=1
where l.bdate between "2015-04-01" and "2016-03-31"
and p.typearea in (1,3) and p.discharge = 9
group by p.hospcode,p.pid
) a on a.hospcode=h.hoscode