select
village.villcode,
villname
,sum(case when getagemonth(person.birth,'2015-07-01') between 0 and 1 and person.typelive in ('1','3') THEN 1 ELSE '' END) AS '0 ด.-1 ด'
,sum(case when getagemonth(person.birth,'2015-07-01') between 1 and 2 and person.typelive in ('1','3') THEN 1 ELSE '' END) AS '1ด.-2ด'
,sum(case when getagemonth(person.birth,'2015-07-01') between 3 and 4 and person.typelive in ('1','3') THEN 1 ELSE '' END) AS '3ด.-4ด'
,sum(case when getagemonth(person.birth,'2015-07-01') between 5 and 6 and person.typelive in ('1','3') THEN 1 ELSE '' END) AS '5ด.-6ด'
,sum(case when getagemonth(person.birth,'2015-07-01') between 7 and 9 and person.typelive in ('1','3') THEN 1 ELSE '' END) AS '7ด.-9ด'
,sum(case when getagemonth(person.birth,'2015-07-01')='9' and person.typelive in ('1','3') THEN 1 ELSE '' END) AS '9ด'
,sum(case when getagemonth(person.birth,'2015-07-01') between 10 and 12 and person.typelive in ('1','3') THEN 1 ELSE '' END) AS '10ด.-12ด'
,sum(case when getagemonth(person.birth,'2015-07-01') between 13 and 15 and person.typelive in ('1','3') THEN 1 ELSE '' END) AS '13ด.-15ด'
,sum(case when getagemonth(person.birth,'2015-07-01') between 16 and 18 and person.typelive in ('1','3') THEN 1 ELSE '' END) AS '16ด.-18ด'
,sum(case when getagemonth(person.birth,'2015-07-01')='18' and person.typelive in ('1','3') THEN 1 ELSE '' END) AS '18ด'
,sum(case when getagemonth(person.birth,'2015-07-01') between 19 and 24 and person.typelive in ('1','3') THEN 1 ELSE '' END) AS '19ด.-24ด'
,sum(case when getagemonth(person.birth,'2015-07-01') between 25 and 30 and person.typelive in ('1','3') THEN 1 ELSE '' END) AS '25ด.-30ด'
,sum(case when getagemonth(person.birth,'2015-07-01')='30' and person.typelive in ('1','3') THEN 1 ELSE '' END) AS '30ด'
,sum(case when getagemonth(person.birth,'2015-07-01') between 31 and 36 and person.typelive in ('1','3') THEN 1 ELSE '' END) AS '31ด.-36ด'
,sum(case when getagemonth(person.birth,'2015-07-01') between 37 and 42 and person.typelive in ('1','3') THEN 1 ELSE '' END) AS '37ด.-42ด'
,sum(case when getagemonth(person.birth,'2015-07-01')='42' and person.typelive in ('1','3') THEN 1 ELSE '' END) AS '42ด'
,sum(case when getagemonth(person.birth,'2015-07-01') between 43 and 48 and person.typelive in ('1','3') THEN 1 ELSE '' END) AS '43ด.-48ด'
,sum(case when getagemonth(person.birth,'2015-07-01') between 49 and 54 and person.typelive in ('1','3') THEN 1 ELSE '' END) AS '49ด.-54ด'
,sum(case when getagemonth(person.birth,'2015-07-01') between 55 and 60 and person.typelive in ('1','3') THEN 1 ELSE '' END) AS '55ด.-60ด'
from person inner join house on person.pcucodeperson = house.pcucode and person.hcode = house.hcode
INNER JOIN village ON house.pcucode = village.pcucode AND house.villcode = village.villcode

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 right(village.villcode,2)<>'00'
group by villname
ORDER BY villcode;