อาจจะบ้านๆ หน่อยน่ะครับ นับเดือน ไล่ไปเลย select pt.*,ov.*
from vn_stat ov
inner join patient pt on pt.hn = ov.hn
inner join ovst on ovst.vn = ov.vn
where ov.vstdate between '2010-09-01' and '2010-09-10' and ov.spclty = '01'
แล้วก็เปลี่ยนช่วงปีเอง
and timestampdiff(MONTH,pt.birthday,ov.vstdate) between 0 and 12
หรือ
and timestampdiff(MONTH,pt.birthday,ov.vstdate) between 13 and 36
หรือ
and timestampdiff(MONTH,pt.birthday,ov.vstdate) between 37 and 71
หรือ
and timestampdiff(MONTH,pt.birthday,ov.vstdate) between 72 and 155
หรือ
and timestampdiff(MONTH,pt.birthday,ov.vstdate) between 156 and 216
หรือจะนับจำนวนรวม ตามช่วงอายุที่กำหนดเมื่อกี้ก็ select 'ผู้มารับบริการ แยกตามช่วงอายุ' as nn
,sum(case when timestampdiff(MONTH,pt.birthday,ov.vstdate) between 0 and 12 then 1 else 0 end) as 0_1
,sum(case when timestampdiff(MONTH,pt.birthday,ov.vstdate) between 13 and 36 then 1 else 0 end) as 11_3
,sum(case when timestampdiff(MONTH,pt.birthday,ov.vstdate) between 37 and 71 then 1 else 0 end) as 31_511
,sum(case when timestampdiff(MONTH,pt.birthday,ov.vstdate) between 72 and 155 then 1 else 0 end) as 6_1211
,sum(case when timestampdiff(MONTH,pt.birthday,ov.vstdate) between 156 and 216 then 1 else 0 end) as 13_18
from vn_stat ov
inner join patient pt on pt.hn = ov.hn
inner join ovst on ovst.vn = ov.vn
where ov.vstdate between '2010-09-01' and '2010-09-10' and ov.spclty = '01'