ลองอันใหม่ครับ
set @t1 = unix_timestamp('2008-10-01');
set @t2 = unix_timestamp('2009-09-30');
select "OPD" as type, year(vstdate)+543 year, case month(vstdate)
when '1' then 'ม.ค.'
when '2' then 'ก.พ.'
when '3' then 'มี.ค.'
when '4' then 'เม.ย.'
when '5' then 'พ.ค.'
when '6' then 'มิ.ย.'
when '7' then 'ก.ค.'
when '8' then 'ส.ค.'
when '9' then 'ก.ย.'
when '10' then 'ต.ค.'
when '11' then 'พ.ย.'
when '12' then 'ธ.ค.'
end as month, count(distinct hn) as count_hn , count(vn) as count
from vn_stat
where unix_timestamp(vstdate) between @t1 and @t2
group by year(vstdate), month(vstdate)
union
select "IPD" as type, year(regdate)+543 year, case month(regdate)
when '1' then 'ม.ค.'
when '2' then 'ก.พ.'
when '3' then 'มี.ค.'
when '4' then 'เม.ย.'
when '5' then 'พ.ค.'
when '6' then 'มิ.ย.'
when '7' then 'ก.ค.'
when '8' then 'ส.ค.'
when '9' then 'ก.ย.'
when '10' then 'ต.ค.'
when '11' then 'พ.ย.'
when '12' then 'ธ.ค.'
end as month, count(distinct hn) as count_hn , count(an) as count
from an_stat
where unix_timestamp(regdate) between @t1 and @t2
group by year(regdate), month(regdate)