SELECT
village.villname,
sum(case when getAgeYearNum(p.birth,'2013-07-01') <5 THEN 1 ELSE '' END) AS '0-4 รวม',
sum(case when getAgeYearNum(p.birth,'2013-07-01') <5 and p.sex='1' THEN 1 ELSE '' END) AS '0-4 ชาย',
sum(case when getAgeYearNum(p.birth,'2013-07-01') <5 and p.sex='2' THEN 1 ELSE '' END) AS '0-4 หญิง',
sum(case when getAgeYearNum(p.birth,'2013-07-01') BETWEEN 5 and 9 THEN 1 ELSE '' END) AS '5-9 รวม',
sum(case when getAgeYearNum(p.birth,'2013-07-01') BETWEEN 5 and 9 and p.sex='1' THEN 1 ELSE '' END) AS '5-9 ชาย',
sum(case when getAgeYearNum(p.birth,'2013-07-01') BETWEEN 5 and 9 and p.sex='2' THEN 1 ELSE '' END) AS '5-9 หญิง',
sum(case when getAgeYearNum(p.birth,'2013-07-01') BETWEEN 10 and 14  THEN 1 ELSE '' END) AS '10-14 รวม',
sum(case when getAgeYearNum(p.birth,'2013-07-01') BETWEEN 10 and 14 and p.sex='1' THEN 1 ELSE '' END) AS '10-14 ชาย',
sum(case when getAgeYearNum(p.birth,'2013-07-01') BETWEEN 10 and 14 and p.sex='2' THEN 1 ELSE '' END) AS '10-14 หญิง',
sum(case when getAgeYearNum(p.birth,'2013-07-01') BETWEEN 15 and 24 THEN 1 ELSE '' END) AS '15-24 รวม',
sum(case when getAgeYearNum(p.birth,'2013-07-01') BETWEEN 15 and 24 and p.sex='1' THEN 1 ELSE '' END) AS '15-24 ชาย',
sum(case when getAgeYearNum(p.birth,'2013-07-01') BETWEEN 15 and 24 and p.sex='2' THEN 1 ELSE '' END) AS '15-24 หญิง',
sum(case when getAgeYearNum(p.birth,'2013-07-01') BETWEEN 25 and 34 THEN 1 ELSE '' END) AS '25-34 รวม',
sum(case when getAgeYearNum(p.birth,'2013-07-01') BETWEEN 25 and 34 and p.sex='1' THEN 1 ELSE '' END) AS '25-34 ชาย',
sum(case when getAgeYearNum(p.birth,'2013-07-01') BETWEEN 25 and 34 and p.sex='2' THEN 1 ELSE '' END) AS '25-34 หญิง',
sum(case when getAgeYearNum(p.birth,'2013-07-01') BETWEEN 35 and 44 THEN 1 ELSE '' END) AS '35-44 รวม',
sum(case when getAgeYearNum(p.birth,'2013-07-01') BETWEEN 35 and 44 and p.sex='1' THEN 1 ELSE '' END) AS '35-44 ชาย',
sum(case when getAgeYearNum(p.birth,'2013-07-01') BETWEEN 35 and 44 and p.sex='2' THEN 1 ELSE '' END) AS '35-44 หญิง',
sum(case when getAgeYearNum(p.birth,'2013-07-01') BETWEEN 45 and 54 THEN 1 ELSE '' END) AS '45-54 รวม',
sum(case when getAgeYearNum(p.birth,'2013-07-01') BETWEEN 45 and 54 and p.sex='1' THEN 1 ELSE '' END) AS '45-54 ชาย',
sum(case when getAgeYearNum(p.birth,'2013-07-01') BETWEEN 45 and 54 and p.sex='2' THEN 1 ELSE '' END) AS '45-54 หญิง',
sum(case when getAgeYearNum(p.birth,'2013-07-01') BETWEEN 55 and 64 THEN 1 ELSE '' END) AS '55-64 รวม',
sum(case when getAgeYearNum(p.birth,'2013-07-01') BETWEEN 55 and 64 and p.sex='1' THEN 1 ELSE '' END) AS '55-64 ชาย',
sum(case when getAgeYearNum(p.birth,'2013-07-01') BETWEEN 55 and 64 and p.sex='2' THEN 1 ELSE '' END) AS '55-64 หญิง',
sum(case when getAgeYearNum(p.birth,'2013-07-01') >65 THEN 1 ELSE '' END) AS '65+ รวม',
sum(case when getAgeYearNum(p.birth,'2013-07-01') >65  and p.sex='1' THEN 1 ELSE '' END) AS '65+ ชาย',
sum(case when getAgeYearNum(p.birth,'2013-07-01') >65  and p.sex='2' THEN 1 ELSE '' END) AS '65+ หญิง',
COUNT(*) 

‪#‎IF‬(p.sex=1,'ชาย',IF(p.sex=2,'หญิง','ไม่ระบุุุ')) AS sex
FROM
person AS p
INNER JOIN ctitle AS t ON p.prename = t.titlecode
INNER JOIN house ON house.pcucode = p.pcucodeperson AND house.hcode = p.hcode
INNER JOIN village ON village.pcucode = house.pcucode AND village.villcode = house.villcode
WHERE birth<'2013-07-01' and SUBSTRING(house.villcode,7,2) <> '00' AND p.typelive IN ('0','1','2')and concat(p.pcucodeperson,
p.pid) not in (SELECT
concat(persondeath.pcucodeperson,persondeath.pid)
FROM
persondeath
WHERE
persondeath.deaddate < '2013-07-01' OR
persondeath.deaddate IS NULL)
GROUP BY village.villcode