select cdiseasechronic.groupname as grname,
case when person.sex = '1' then 'ชาย'when person.sex = '2' then 'หญิง'else'ไม่ระบุ'end as grsex,
sum(case when GetAgeYearNum(person.birth,'2013-10-01') <15 and person.typelive in ('1','3') then 1 else '' end) as '<15ปี',
sum(case when GetAgeYearNum(person.birth,'2013-10-01') between 15 and 34 and person.typelive in ('1','3') then 1 else '' end) as '15-34ปี',
sum(case when GetAgeYearNum(person.birth,'2013-10-01') between 35 and 59 and person.typelive in ('1','3') then 1 else '' end) as '35-59ปี',
sum(case when GetAgeYearNum(person.birth,'2013-10-01') >= 60 and person.typelive in ('1','3') then 1 else '' end) as '>60ปี'

from
personchronic inner join person on personchronic.pcucodeperson = person.pcucodeperson
and personchronic.pid = person.pid
inner join cdisease on personchronic.chroniccode = cdisease.diseasecode
inner join cdiseasechronic on cdisease.codechronic = cdiseasechronic.groupcode
inner join house on person.pcucodeperson = house.pcucode and person.hcode = house.hcode
where concat(person.pid,person.pcucodeperson)not in (select concat(persondeath.pid,persondeath.pcucodeperson)
from persondeath where deaddate<=curdate() or deaddate is not null) and SUBSTRING(house.villcode,7,2) = '01'
group by grname,grsex;