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
,bweight
,if(bweight<2500,"Y",null) result
from person p
join newborn nb on nb.pid=p.pid and nb.hospcode=p.hospcode
where nb.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
,bweight
,if(bweight<2500,"Y",null) result
from person p
join newborn nb on nb.pid=p.pid and nb.hospcode=p.hospcode
where nb.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