set @ds = "2013-10-01";
set @de = "2014-02-28";

SELECT p.pcucodeperson,
SUM(case when p.typelive IN (1,3)AND(year(now())- year(p.birth) BETWEEN 15 AND 34) THEN 1 END) AS ประชากร15
,SUM(case when p.typelive IN (1,3)AND(year(now())- year(p.birth) BETWEEN 35 AND 59) THEN 1 END) AS ประชากร35
,SUM(case when p.typelive IN (1,3)AND(year(now())- year(p.birth) BETWEEN 60 AND 120) THEN 1 END) AS ประชากร60 

 
FROM ncdpersonscreenall nc
INNER JOIN person p ON nc.pid=p.pid AND nc.pcucode=p.pcucodeperson
LEFT JOIN persondeath pd ON p.pid=pd.pid

WHERE  nc.bsl IS NOT NULL AND pd.pid IS NULL AND nc.screen_date  BETWEEN @ds AND @de AND p.nation ='99'

GROUP BY p.pcucodeperson ;
