BMS-HOSxP Community

HOSxP => Report Exchange => ข้อความที่เริ่มโดย: panus_t ที่ ตุลาคม 08, 2012, 13:32:09 PM

หัวข้อ: กรองอายุ 15-34 ปีจากตาราง patient ได้ยังไงครับ
เริ่มหัวข้อโดย: panus_t ที่ ตุลาคม 08, 2012, 13:32:09 PM
##select (2012 - YEAR(`birthday`)) as age_y from patient   where  chwpart="30" and amppart="03" and tmbpart="01" and moopart not in ('2','5','13','16') and age_y between "15" and "34"

 ;D
หัวข้อ: Re: กรองอายุ 15-34 ปีจากตาราง patient ได้ยังไงครับ
เริ่มหัวข้อโดย: manoi ที่ ตุลาคม 08, 2012, 13:38:11 PM
ทำไมไม่เขียนเป็น

where birthday between d1 and d2 จะง่ายกว่าหรือเปล่าครับ
หัวข้อ: Re: กรองอายุ 15-34 ปีจากตาราง patient ได้ยังไงครับ
เริ่มหัวข้อโดย: panus_t ที่ ตุลาคม 08, 2012, 14:15:39 PM
เผื่อมีการเปลี่ยนแปลงวันที่ตัดยอด เช่น อายุ 35 ปี ณวันที่ 1 ม.ค.55
กลัวจะคำนวนช่วงอายุผิด (อาจจะเบลอ ๆ ครับ) ก็เลยคิดว่าถ้าเป็นอัตโนมัติก็ดี ใส่ วันที่ตัดยอด ใส่ช่วงอายุ แบบนี้ครับ.... ;D แต่ตอนนี้ผมก็ทำแบบอ. MN ไปก่อน (ตอนนี้สมองก็มึน ๆ แล้วกลัวคำนวนกำหนดช่วงอายุผิดเหมือนกัน)   ;D
หัวข้อ: Re: กรองอายุ 15-34 ปีจากตาราง patient ได้ยังไงครับ
เริ่มหัวข้อโดย: udomchok ที่ ตุลาคม 08, 2012, 15:04:33 PM
select hn, birthday, datediff(now(),birthday)/365.25 as age_y
from patient
where datediff(now(),birthday)/365.25 between 15 and 34
order by age_y

สามารถเปลี่ยน now() เป็นวันที่ที่ต้องการ เช่น
ต้องการคำนวณอายุถึงวันที่ 1 ม.ค. 55

select hn, birthday, datediff("2012-01-01",birthday)/365.25 as age_y
from patient
where datediff("2012-01-01",birthday)/365.25 between 15 and 34
order by age_y
หัวข้อ: Re: กรองอายุ 15-34 ปีจากตาราง patient ได้ยังไงครับ
เริ่มหัวข้อโดย: woravet ที่ ตุลาคม 09, 2012, 10:36:22 AM
set @ds:='2012-1-1';
select hn,birthday,timestampdiff(year,birthday,@ds) 'age_y'
,addrpart,lpad(moopart,2,'0') moopart,tmbpart,amppart,chwpart
from patient p
where timestampdiff(year,birthday,@ds) between 15 and 34
and chwpart=62
and amppart=10
and tmbpart=03
and moopart in (3,4,7,8)