BMS-HOSxP Community

HOSxP => การเขียน SQL Script => ข้อความที่เริ่มโดย: Apiruk_Jbhos ที่ มีนาคม 12, 2010, 15:34:06 PM

หัวข้อ: อยากรบกวนให้ ช่วยเขียน SQL ให้หน่อยครับ
เริ่มหัวข้อโดย: Apiruk_Jbhos ที่ มีนาคม 12, 2010, 15:34:06 PM
สวัสดีครับ พอดีที่ รพ.เขาอยากจะได้ 10 อันดับ โรค ของ ตำบล รายหมู๋ แต่ Code ที่เขียนมันเป็น รายครั้งครับ

select a.pdx,count(a.pdx) as pdx_count ,i.name as icdname,a.aid as aid ,a.moopart as moo
from vn_stat a
left outer join icd101 i on i.code=a.main_pdx
where a.vstdate between '2006-01-01' and '2010-03-12'
and a.pdx<>'' and a.pdx is not null
and a.aid = "700201"
and a.moopart = "1"
group by a.pdx,i.name
order by pdx_count desc
limit 10

ก็ออกมา เยอะหน่อย
แบบนี้ครับ
pdx   pdx_count   icdname
I10   1311   HT, Hypertension (Essential (primary))
Z480   1065   การดูแลติดตามทางศัลยกรรมอื่น
E119   654   Non-insulin-dependent diabetes mellitus
J029   570   Acute pharyngitis
Z501   191   การดูแลที่เกี่ยวข้องกับหัตถการฟื้นฟูสภาพ
K30   151   Dyspepsia
K029   143   Dental caries
Z242   134   ความต้องการการสร้างเสริมภูมิคุ้มกันต่อโรคติดเชื้อไวรัสโรคใดโรคหนึ่ง
K0402   117   Diseases of pulp and periapical tissues
Z300   110   การรับบริการคุมกำเนิด


แต่ อยากได้เป็น รายคน ครับ ประมาร ว่า ตัด HN ซ้ำออกครับ แต่ พอเอา HN ไปใส่ใน Group แล้วมัน ออกมา ผิดครับ
กลับออกมาแบบนี้ครับ
select a.pdx,count(a.pdx) as pdx_count ,i.name as icdname,a.aid as aid ,a.moopart as moo
from vn_stat a
left outer join icd101 i on i.code=a.main_pdx
where a.vstdate between '2006-01-01' and '2010-03-12'
and a.pdx<>'' and a.pdx is not null
and a.aid = "700201"
and a.moopart = "1"
group by a.pdx,i.name,a.hn
order by pdx_count desc
limit 10
ออกมาแบบนี้ครับ

pdx   pdx_count   icdname
Z480   93   การดูแลติดตามทางศัลยกรรมอื่น
Z501   41   การดูแลที่เกี่ยวข้องกับหัตถการฟื้นฟูสภาพ
I10   36   HT, Hypertension (Essential (primary))
I10   36   HT, Hypertension (Essential (primary))
I10   36   HT, Hypertension (Essential (primary))
I10   35   HT, Hypertension (Essential (primary))
Z480   35   การดูแลติดตามทางศัลยกรรมอื่น
B24   35   Unspecified human immunodeficiency virus(HIV) disease
I10   34   HT, Hypertension (Essential (primary))
I10   34   HT, Hypertension (Essential (primary))
ไงกรุณาช่วยดูให้หน่อยครับ
หัวข้อ: Re: อยากรบกวนให้ ช่วยเขียน SQL ให้หน่อยครับ
เริ่มหัวข้อโดย: redfireball ที่ มีนาคม 13, 2010, 02:57:18 AM
set @d1 = '2010-01-01';
set @d2 = '2010-01-31';
select concat(t.name,' หมู่ ',a0.moopart) as tmb_moo
,(select a1.pdx from vn_stat a1 where a1.vstdate between @d1 and @d2 and a1.moopart = a0.moopart and a1.aid = a0.aid and (a1.pdx<>'' or a1.pdx is not null) group by a1.pdx order by count(*) desc, a1.pdx limit 1) as no1
,(select a2.pdx from vn_stat a2 where a2.vstdate between @d1 and @d2 and a2.moopart = a0.moopart and a2.aid = a0.aid and (a2.pdx<>'' or a2.pdx is not null) group by a2.pdx order by count(*) desc, a2.pdx limit 1,1) as no2
,(select a3.pdx from vn_stat a3 where a3.vstdate between @d1 and @d2 and a3.moopart = a0.moopart and a3.aid = a0.aid and (a3.pdx<>'' or a3.pdx is not null) group by a3.pdx order by count(*) desc, a3.pdx limit 2,1) as no3
,(select a4.pdx from vn_stat a4 where a4.vstdate between @d1 and @d2 and a4.moopart = a0.moopart and a4.aid = a0.aid and (a4.pdx<>'' or a4.pdx is not null) group by a4.pdx order by count(*) desc, a4.pdx limit 3,1) as no4
,(select a5.pdx from vn_stat a5 where a5.vstdate between @d1 and @d2 and a5.moopart = a0.moopart and a5.aid = a0.aid and (a5.pdx<>'' or a5.pdx is not null) group by a5.pdx order by count(*) desc, a5.pdx limit 4,1) as no5
,(select a6.pdx from vn_stat a6 where a6.vstdate between @d1 and @d2 and a6.moopart = a0.moopart and a6.aid = a0.aid and (a6.pdx<>'' or a6.pdx is not null) group by a6.pdx order by count(*) desc, a6.pdx limit 5,1) as no6
,(select a7.pdx from vn_stat a7 where a7.vstdate between @d1 and @d2 and a7.moopart = a0.moopart and a7.aid = a0.aid and (a7.pdx<>'' or a7.pdx is not null) group by a7.pdx order by count(*) desc, a7.pdx limit 6,1) as no7
,(select a8.pdx from vn_stat a8 where a8.vstdate between @d1 and @d2 and a8.moopart = a0.moopart and a8.aid = a0.aid and (a8.pdx<>'' or a8.pdx is not null) group by a8.pdx order by count(*) desc, a8.pdx limit 7,1) as no8
,(select a9.pdx from vn_stat a9 where a9.vstdate between @d1 and @d2 and a9.moopart = a0.moopart and a9.aid = a0.aid and (a9.pdx<>'' or a9.pdx is not null) group by a9.pdx order by count(*) desc, a9.pdx limit 8,1) as no9
,(select a10.pdx from vn_stat a10 where a10.vstdate between @d1 and @d2 and a10.moopart = a0.moopart and a10.aid = a0.aid and (a10.pdx<>'' or a10.pdx is not null) group by a10.pdx order by count(*) desc, a10.pdx limit 9,1) as no10
from vn_stat a0
left join thaiaddress t on a0.aid = t.addressid
where a0.aid = '430801' and a0.moopart in ('1','2','3','4','5','6')
and a0.vstdate between @d1 and @d2
group by a0.moopart;

แบบนี้ พอไหวไมครับ  ;D ;D ;D
หัวข้อ: Re: อยากรบกวนให้ ช่วยเขียน SQL ให้หน่อยครับ
เริ่มหัวข้อโดย: เกื้อกูล ครับ.. ที่ มีนาคม 13, 2010, 08:42:41 AM
แบบนี้ พอไหวไมครับ  ;D ;D ;D


อย่างนี้ เรียกว่า เยี่ยมครับ...ท่าน
หัวข้อ: Re: อยากรบกวนให้ ช่วยเขียน SQL ให้หน่อยครับ
เริ่มหัวข้อโดย: E-Hos ที่ มีนาคม 13, 2010, 08:53:44 AM
แบบนี้ พอไหวไมครับ  ;D ;D ;D


อย่างนี้ เรียกว่า เยี่ยมครับ...ท่าน

สุดยอดครับ อ. ;D ;D ;D ;D
หัวข้อ: Re: อยากรบกวนให้ ช่วยเขียน SQL ให้หน่อยครับ
เริ่มหัวข้อโดย: udomchok ที่ มีนาคม 13, 2010, 11:34:49 AM
สวัสดีครับ พอดีที่ รพ.เขาอยากจะได้ 10 อันดับ โรค ของ ตำบล รายหมู๋ แต่ Code ที่เขียนมันเป็น รายครั้งครับ

select a.pdx,count(a.pdx) as pdx_count ,i.name as icdname,a.aid as aid ,a.moopart as moo
from vn_stat a
left outer join icd101 i on i.code=a.main_pdx
where a.vstdate between '2006-01-01' and '2010-03-12'
and a.pdx<>'' and a.pdx is not null
and a.aid = "700201"
and a.moopart = "1"
group by a.pdx,i.name
order by pdx_count desc
limit 10

ก็ออกมา เยอะหน่อย
แบบนี้ครับ
pdx   pdx_count   icdname
I10   1311   HT, Hypertension (Essential (primary))
Z480   1065   การดูแลติดตามทางศัลยกรรมอื่น
E119   654   Non-insulin-dependent diabetes mellitus
J029   570   Acute pharyngitis
Z501   191   การดูแลที่เกี่ยวข้องกับหัตถการฟื้นฟูสภาพ
K30   151   Dyspepsia
K029   143   Dental caries
Z242   134   ความต้องการการสร้างเสริมภูมิคุ้มกันต่อโรคติดเชื้อไวรัสโรคใดโรคหนึ่ง
K0402   117   Diseases of pulp and periapical tissues
Z300   110   การรับบริการคุมกำเนิด


แต่ อยากได้เป็น รายคน ครับ ประมาร ว่า ตัด HN ซ้ำออกครับ แต่ พอเอา HN ไปใส่ใน Group แล้วมัน ออกมา ผิดครับ
กลับออกมาแบบนี้ครับ
select a.pdx,count(a.pdx) as pdx_count ,i.name as icdname,a.aid as aid ,a.moopart as moo
from vn_stat a
left outer join icd101 i on i.code=a.main_pdx
where a.vstdate between '2006-01-01' and '2010-03-12'
and a.pdx<>'' and a.pdx is not null
and a.aid = "700201"
and a.moopart = "1"
group by a.pdx,i.name,a.hn
order by pdx_count desc
limit 10
ออกมาแบบนี้ครับ

pdx   pdx_count   icdname
Z480   93   การดูแลติดตามทางศัลยกรรมอื่น
Z501   41   การดูแลที่เกี่ยวข้องกับหัตถการฟื้นฟูสภาพ
I10   36   HT, Hypertension (Essential (primary))
I10   36   HT, Hypertension (Essential (primary))
I10   36   HT, Hypertension (Essential (primary))
I10   35   HT, Hypertension (Essential (primary))
Z480   35   การดูแลติดตามทางศัลยกรรมอื่น
B24   35   Unspecified human immunodeficiency virus(HIV) disease
I10   34   HT, Hypertension (Essential (primary))
I10   34   HT, Hypertension (Essential (primary))
ไงกรุณาช่วยดูให้หน่อยครับ
ทำไมนับแต่ pdx ล่ะครับ
ถ้าคนไข้มารับการรักษา DM และ HT ในวันเดียวกัน แล้วเอา DM เป็น pdx ส่วน HT เป็น SDx อย่างนี้ก็หายสิครับ

น่าจะนับจาก ovstdiag จะดีกว่าไหมครับ
select o.icd10, count(o.vn) as cases, i.name
from ovstdiag o
join icd101 i on i.code=o.icd10
where .....
group by o.icd10
order by count(vn)

ถ้านับคนก็ count distinct
select o.icd10, count(distinct o.hn) as cases, i.name
from ovstdiag o
join icd101 i on i.code=o.icd10
where .....
group by o.icd10
order by count(vn)

ประมาณนี้ครับ (พอดียังไม่ได้ลอง run จริง ๆ นะครับ...โปรดระวัง...ด้นสด ๆ)
หัวข้อ: Re: อยากรบกวนให้ ช่วยเขียน SQL ให้หน่อยครับ
เริ่มหัวข้อโดย: Bond_007 ที่ มีนาคม 13, 2010, 13:56:40 PM
 ;D :o :o
หัวข้อ: Re: อยากรบกวนให้ ช่วยเขียน SQL ให้หน่อยครับ
เริ่มหัวข้อโดย: Svl2Nuk3 ที่ มีนาคม 13, 2010, 20:49:12 PM
ดูคร่าวๆ  ถ้าทำเป็น  cross tab  report  น่าจะง่ายกว่านะครับ
 ;D
หัวข้อ: Re: อยากรบกวนให้ ช่วยเขียน SQL ให้หน่อยครับ
เริ่มหัวข้อโดย: doramon ที่ มีนาคม 13, 2010, 23:42:51 PM
น่าจะนับจาก ovstdiag จะดีกว่าไหมครับ


ตารางนี้น่าจะดีถูกมากกว่า vn_stat