SELECT
convert(concat('หมู่ ',village.villno,':',village.villname) using utf8 ) as หมู่บ้าน,
SUM(case when person.sex = 2 and person.marystatus = 2 THEN 1 ELSE 0 end) as 'หญิงอายุ 15 - 49 ปี ที่มีคู่'
FROM
house
INNER JOIN person ON person.pcucodeperson = house.pcucode AND person.hcode = house.hcode
LEFT JOIN women ON women.pid = person.pid AND women.pcucodeperson = person.pcucodeperson
INNER JOIN village ON house.pcucode = village.pcucode AND house.villcode = village.villcode
WHERE
RIGHT(village.villcode,2) <> 00 AND
person.typelive IN (1, 3) AND
person.dischargetype = 9 AND
person.sex = 2 AND
person.marystatus = 2 AND
getAgeYearNum(birth,CURDATE())BETWEEN '15' and '49'
GROUP BY
village.villname
UNION
SELECT
'รวม',
SUM(case when person.sex = 2 and person.marystatus = 2 THEN 1 ELSE 0 end) as 'หญิงอายุ 15 - 49 ปี ที่มีคู่'
FROM
house
INNER JOIN person ON person.pcucodeperson = house.pcucode AND person.hcode = house.hcode
LEFT JOIN women ON women.pid = person.pid AND women.pcucodeperson = person.pcucodeperson
INNER JOIN village ON house.pcucode = village.pcucode AND house.villcode = village.villcode
WHERE
RIGHT(village.villcode,2) <> 00 AND
person.typelive IN (1, 3) AND
person.dischargetype = 9 AND
person.sex = 2 AND
person.marystatus = 2 AND
getAgeYearNum(birth,'20141001')BETWEEN '15' and '49'
ORDER BY 'หมู่บ้าน';