BMS-HOSxP Community

HOSxP => การเขียน SQL Script => ข้อความที่เริ่มโดย: d^_^b►►ทิว ที่ ธันวาคม 01, 2010, 17:06:37 PM

หัวข้อ: ขอคำแนะนำการใช้ have
เริ่มหัวข้อโดย: d^_^b►►ทิว ที่ ธันวาคม 01, 2010, 17:06:37 PM
ขอคำแนะนำการใช้ have ในสคริป sql หน่อยครับ
หัวข้อ: Re: ขอคำแนะนำการใช้ have
เริ่มหัวข้อโดย: Svl2Nuk3 ที่ ธันวาคม 03, 2010, 16:23:15 PM
ใช่ตัว  having หรือเปล่าครับ

ตัวนี้เราจะใช้ในการกำหนดเงื่อนไขของฟังก์ชัน เช่น sum , avg , count

ปกติเราใส่เงื่อนไข  เราจะใส่ตรง  where  แต่ถ้าเงื่อนไขที่เรากำหนดเป็นฟังก์ชัน  เราต้องใช้ having ครับ 

ดูตัวอย่างจากโค้ดนี้ก็ได้ครับ

select hn,count(*) as cc,sum(income) as ss,age_y from an_stat
where dchdate between "2010-05-01" and "2010-05-30"
group by hn
order by cc desc
limit 10

โค้ดนี้เป็นการดึงข้อมูลผู้ป่วยใน  10  อันดับแรกที่  ดิสชาจ ในเดือน พฤษภา บ่อยที่สุด
โดยดึง  hn , จำนวนครั้งที่มา dischage , รวมค่ารักษาของคนนั้น , อายุ


ถ้าเราจะกรองอายุ  ก็ต้องใส่เงื่อนไขใน where  เช่นจะเอาอายุมากกว่่า  10  ปี  โค้ดก็จะเป็นดังนี้
select hn,count(*) as cc,sum(income) as ss,age_y from an_stat
where dchdate between "2010-05-01" and "2010-05-30" and age_y > 10
group by hn
order by cc desc
limit 10


แต่ถ้าเราจะกรองค่าใช้จ่ายรวมที่มากกว่า  5000  ต้องใช้  having เนื่องจากฟิลนี้ใช้ฟังก์ชัน  sum
select hn,count(*) as cc,sum(income) as ss,age_y from an_stat
where dchdate between "2010-05-01" and "2010-05-30"
group by hn
having ss > 5000
order by cc desc
limit 10


หัวข้อ: Re: ขอคำแนะนำการใช้ have
เริ่มหัวข้อโดย: boomjang400d ที่ ธันวาคม 09, 2010, 11:31:44 AM
ใช่ตัว  having หรือเปล่าครับ

ตัวนี้เราจะใช้ในการกำหนดเงื่อนไขของฟังก์ชัน เช่น sum , avg , count

ปกติเราใส่เงื่อนไข  เราจะใส่ตรง  where  แต่ถ้าเงื่อนไขที่เรากำหนดเป็นฟังก์ชัน  เราต้องใช้ having ครับ 

ดูตัวอย่างจากโค้ดนี้ก็ได้ครับ

select hn,count(*) as cc,sum(income) as ss,age_y from an_stat
where dchdate between "2010-05-01" and "2010-05-30"
group by hn
order by cc desc
limit 10

โค้ดนี้เป็นการดึงข้อมูลผู้ป่วยใน  10  อันดับแรกที่  ดิสชาจ ในเดือน พฤษภา บ่อยที่สุด
โดยดึง  hn , จำนวนครั้งที่มา dischage , รวมค่ารักษาของคนนั้น , อายุ


ถ้าเราจะกรองอายุ  ก็ต้องใส่เงื่อนไขใน where  เช่นจะเอาอายุมากกว่่า  10  ปี  โค้ดก็จะเป็นดังนี้
select hn,count(*) as cc,sum(income) as ss,age_y from an_stat
where dchdate between "2010-05-01" and "2010-05-30" and age_y > 10
group by hn
order by cc desc
limit 10


แต่ถ้าเราจะกรองค่าใช้จ่ายรวมที่มากกว่า  5000  ต้องใช้  having เนื่องจากฟิลนี้ใช้ฟังก์ชัน  sum
select hn,count(*) as cc,sum(income) as ss,age_y from an_stat
where dchdate between "2010-05-01" and "2010-05-30"
group by hn
having ss > 5000
order by cc desc
limit 10




โหวตให้ 1 like ครับ ;D ;D ;D ;D