ผู้เขียน หัวข้อ: SQL Query ช่วงอายุ  (อ่าน 16186 ครั้ง)

0 สมาชิก และ 1 บุคคลทั่วไป กำลังดูหัวข้อนี้

ออฟไลน์ Bluebird

  • Hero Member
  • *****
  • กระทู้: 1,062
  • Respect: +2
    • ดูรายละเอียด
    • โรงพยาบาลระแงะ จังหวัดนราธิวาส
SQL Query ช่วงอายุ
« เมื่อ: กุมภาพันธ์ 05, 2013, 12:29:50 PM »
0
อยากทราบว่า Query ช่วงอายุ  ดึงจากตาราง vn_stat แต่ละช่วง มีดังนี้
 9  - 12 เดือน  หมายถึง เด็กอายุตั้งแต่ 0 ปี 9 เดือน 1 วัน ถึง 1 ปี 0 เดือน 0 วัน

12 - 18 เดือน  หมายถึง เด็กอายุตั้งแต่ 1 ปี 0 เดือน 1 วัน ถึง 1 ปี 6 เดือน 0 วัน

18 - 24 เดือน  หมายถึง เด็กอายุตั้งแต่ 1 ปี 6 เดือน 1 วัน  ถึง 2 ปี 0 เดือน 0 วัน

24 - 48 เดือน  หมายถึง เด็กอายุตั้งแต่ 2 ปี 0 เดือน 1 วัน  ถึง 4 ปี 0 เดือน 0 วัน


อยากรู้ว่า Query ช่วงอายุได้แบบไหน



นักวิชาการคอมพิวเตอร์ โรงพยาบาลระแงะ  จังหวัดนราธิวาส
www.rangaehospital.com

เริ่มใช้งานระบบ 1 ตุลาคม 2550
Server     :  HP Proliant DL180G6 RAM 16Gb. 
Replicate  : DELL PowerEdge 1800   RAM 16 Gb.
MySQL 5.5.31
OS CentOS 5 64bit
HOSxP Version : 3.59.4.27(ตาม Version ที่ออกใหม่ล่าสุด)

ออฟไลน์ armds

  • Hero Member
  • *****
  • กระทู้: 1,084
  • armds
  • Respect: +10
    • ดูรายละเอียด
Re: SQL Query ช่วงอายุ
« ตอบกลับ #1 เมื่อ: กุมภาพันธ์ 05, 2013, 14:17:25 PM »
0
หรือลองดูแบบนี้ครับ
select age_y,count(distinct hn) as c_hn,count(*) as c_vn from vn_stat
where vstdate between'2013-01-01' and '2013-02-05'
and age_y = 0 and age_m between'9'and'12'
union
select age_y,count(distinct hn) as c_hn,count(*) as c_vn from vn_stat
where vstdate between'2013-01-01' and '2013-02-05'
and age_y = 1 and age_m between'0'and'6'  and age_d = 0
union
select age_y,count(distinct hn) as c_hn,count(*) as c_vn from vn_stat
where vstdate between'2013-01-01' and '2013-02-05'
and age_y between 1 and 2 and age_m between'6'and'12'
union
select age_y,count(distinct hn) as c_hn,count(*) as c_vn from vn_stat
where vstdate between'2013-01-01' and '2013-02-05'
and age_y between 2 and 4 and age_m < 1
โรงพยาบาลสมเด็จพระยุพราชด่านซ้าย จ.เลย
ขนาด 60 เตียง นวก. คอมพิวเตอร์ ขึ้นระบบ ปี 2548
ขอบคุณ อ.ชัยพร อ.สุชัย อ.เดชา อ.doreamon อ.naj อ.ขวด และอ.ในเว็บ hosxp.net ทุกท่าน

ออฟไลน์ Bluebird

  • Hero Member
  • *****
  • กระทู้: 1,062
  • Respect: +2
    • ดูรายละเอียด
    • โรงพยาบาลระแงะ จังหวัดนราธิวาส
Re: SQL Query ช่วงอายุ
« ตอบกลับ #2 เมื่อ: กุมภาพันธ์ 05, 2013, 14:19:06 PM »
0
ลองคำนวณเป็นเดือน จะง่ายดีกว่าไหมครับ (เปลี่ยนช่วงเดือนเอานะคับ)

SELECT v.hn, v.age_y, v.age_m, v.age_d,
   TIMESTAMPDIFF(month,p.birthday,v.vstdate) as month_total
from vn_stat v
INNER JOIN patient p ON p.hn = v.hn
WHERE (TIMESTAMPDIFF(month,p.birthday,v.vstdate)) BETWEEN 9 AND 12


หรือแบบตารางเดี่ยว

SELECT v.hn, v.age_y, v.age_m, v.age_d,
   ((v.age_y*12)+v.age_m) as month_total
from vn_stat v
WHERE ((v.age_y*12)+v.age_m) BETWEEN 9 AND 12 


หากเงื่อนไข แค่เดือน พอไหว แต่นี่ แพทย์ ต้องการ เงื่อนไขวันด้วย

เช่น หาก 12 เืดือน ซึ่งมันก็คือ 1 ปี แต่ ถ้า เกิน แค่วันเดียว จะให้ แสดงช่วง 12-18 ต่อ
      
นักวิชาการคอมพิวเตอร์ โรงพยาบาลระแงะ  จังหวัดนราธิวาส
www.rangaehospital.com

เริ่มใช้งานระบบ 1 ตุลาคม 2550
Server     :  HP Proliant DL180G6 RAM 16Gb. 
Replicate  : DELL PowerEdge 1800   RAM 16 Gb.
MySQL 5.5.31
OS CentOS 5 64bit
HOSxP Version : 3.59.4.27(ตาม Version ที่ออกใหม่ล่าสุด)

ออฟไลน์ Bluebird

  • Hero Member
  • *****
  • กระทู้: 1,062
  • Respect: +2
    • ดูรายละเอียด
    • โรงพยาบาลระแงะ จังหวัดนราธิวาส
Re: SQL Query ช่วงอายุ
« ตอบกลับ #3 เมื่อ: กุมภาพันธ์ 05, 2013, 14:30:43 PM »
0
หรือลองดูแบบนี้ครับ
age_y = 0 and age_m between'9'and'12'
เงื่อนไขนี้ ได้เฉพาะ 9 เดือน ถึง 11 เดือน กว่าเอง
แต่ แพทย์ต้องการ ถ้า อายุ 1 ปี พอดี จะอยู่ในช่วง 9-12 เดือนด้วย ซึ่งมันก็คือ
age_y=1 and age_m=0 and age_d=0
นักวิชาการคอมพิวเตอร์ โรงพยาบาลระแงะ  จังหวัดนราธิวาส
www.rangaehospital.com

เริ่มใช้งานระบบ 1 ตุลาคม 2550
Server     :  HP Proliant DL180G6 RAM 16Gb. 
Replicate  : DELL PowerEdge 1800   RAM 16 Gb.
MySQL 5.5.31
OS CentOS 5 64bit
HOSxP Version : 3.59.4.27(ตาม Version ที่ออกใหม่ล่าสุด)

ออฟไลน์ armds

  • Hero Member
  • *****
  • กระทู้: 1,084
  • armds
  • Respect: +10
    • ดูรายละเอียด
Re: SQL Query ช่วงอายุ
« ตอบกลับ #4 เมื่อ: กุมภาพันธ์ 05, 2013, 14:38:35 PM »
0

 เงื่อนไขนี้ ได้เฉพาะ 9 เดือน ถึง 11 เดือน กว่าเอง
แต่ แพทย์ต้องการ ถ้า อายุ 1 ปี พอดี จะอยู่ในช่วง 9-12 เดือนด้วย ซึ่งมันก็คือ
age_y=1 and age_m=0 and age_d=0
ลองแบบนี้ดูครับ
select concat(age_y,age_m,age_d)as cc from vn_stat
where vstdate between'2013-02-01' and '2013-02-05'
and concat(age_y,age_m,age_d) between'090'and'100'
โรงพยาบาลสมเด็จพระยุพราชด่านซ้าย จ.เลย
ขนาด 60 เตียง นวก. คอมพิวเตอร์ ขึ้นระบบ ปี 2548
ขอบคุณ อ.ชัยพร อ.สุชัย อ.เดชา อ.doreamon อ.naj อ.ขวด และอ.ในเว็บ hosxp.net ทุกท่าน

ออฟไลน์ Bluebird

  • Hero Member
  • *****
  • กระทู้: 1,062
  • Respect: +2
    • ดูรายละเอียด
    • โรงพยาบาลระแงะ จังหวัดนราธิวาส
Re: SQL Query ช่วงอายุ
« ตอบกลับ #5 เมื่อ: กุมภาพันธ์ 05, 2013, 15:24:11 PM »
0
เช่น 9-12 เดือน  คือ ((vn.age_y=0 and vn.age_m>8) or (vn.age_y=1 and vn.age_m=0 and vn.age_d=0 )

แต่ 12 เืดือน ถึง 18 เดือน คือ (vn.age_y=1 and vn.age_m=0
or vn.age_y=1 and vn.age_m=1
or vn.age_y=1 and vn.age_m=2
or vn.age_y=1 and vn.age_m=3
or vn.age_y=1 and vn.age_m=4
or vn.age_y=1 and vn.age_m=5
or vn.age_y=1 and vn.age_m=6 and age_d=0)
« แก้ไขครั้งสุดท้าย: กุมภาพันธ์ 05, 2013, 15:50:27 PM โดย Bluebird »
นักวิชาการคอมพิวเตอร์ โรงพยาบาลระแงะ  จังหวัดนราธิวาส
www.rangaehospital.com

เริ่มใช้งานระบบ 1 ตุลาคม 2550
Server     :  HP Proliant DL180G6 RAM 16Gb. 
Replicate  : DELL PowerEdge 1800   RAM 16 Gb.
MySQL 5.5.31
OS CentOS 5 64bit
HOSxP Version : 3.59.4.27(ตาม Version ที่ออกใหม่ล่าสุด)

ออฟไลน์ woravet

  • Sr. Member
  • ****
  • กระทู้: 409
  • Respect: +12
    • ดูรายละเอียด
Re: SQL Query ช่วงอายุ
« ตอบกลับ #6 เมื่อ: กุมภาพันธ์ 06, 2013, 11:28:40 AM »
0
select 'ช่วงอายุ 9-12 เดือน' as 'age'
,count(distinct v.hn) 'case'
,count(v.hn) 'visit'
from vn_stat v
where v.vstdate between '2013-1-1' and '2013-1-31'
and concat(lpad(v.age_y,2,'0'),lpad(v.age_m,2,'0'),lpad(v.age_d,2,'0')) between '000900' and '001200'
union
select 'ช่วงอายุ 12-18 เดือน' as 'age'
,count(distinct v.hn) 'case'
,count(v.hn) 'visit'
from vn_stat v
where v.vstdate between '2013-1-1' and '2013-1-31'
and concat(lpad(v.age_y,2,'0'),lpad(v.age_m,2,'0'),lpad(v.age_d,2,'0')) between '010001' and '010600'
union
select 'ช่วงอายุ 18-24 เดือน' as 'age'
,count(distinct v.hn) 'case'
,count(v.hn) 'visit'
from vn_stat v
where v.vstdate between '2013-1-1' and '2013-1-31'
and concat(lpad(v.age_y,2,'0'),lpad(v.age_m,2,'0'),lpad(v.age_d,2,'0')) between '010601' and '020000'
union
select 'ช่วงอายุ 24-48 เดือน' as 'age'
,count(distinct v.hn) 'case'
,count(v.hn) 'visit'
from vn_stat v
where v.vstdate between '2013-1-1' and '2013-1-31'
and concat(lpad(v.age_y,2,'0'),lpad(v.age_m,2,'0'),lpad(v.age_d,2,'0')) between '020001' and '040000'
แบบของคุณ Armds ก้อดีเหมือนกัน


รพ.บึงสามัคคี จ.กำแพงเพชร
*********************
อ.สุชัยและคณะ ขึ้นระบบ 29 ตค.49(Linux+Fedara 5)
*********************
ปัจจุบัน
HOSxP version 3.53.4.27
Server:Windows XP SP2,MySQL 5
Client ประมาณ 15 เครื่อง