BMS-HOSxP Community

HOSxP => การเขียน SQL Script => ข้อความที่เริ่มโดย: yansue ที่ มิถุนายน 27, 2014, 11:59:55 AM

หัวข้อ: สอบถามคำสั่ง if ซ้อนหลายชั้นหน่อยครับ
เริ่มหัวข้อโดย: yansue ที่ มิถุนายน 27, 2014, 11:59:55 AM
ผมมีโจทก์ว่าจะให้ข้อมูลแสดงเป็น งวดตามไตรมาส โดยใช้คำสั่งนี้ครับ
select month(vstdate),if(month(vstdate) in(10,11,12),if(month(vstdate) in(1,2,3),"1","2"),
if(month(vstdate) in(4,5,6),"3","4"))
from ovst
GROUP BY month(vstdate)
ได้ผลตามภาพครับ งวดที่ 1 กลายเป็น 2 และสองกลายเป็น 4
รบกวนช่วยดูคำสั่งให้หน่อยครับว่าจะแสดงข้อมูลตามที่ต้องการได้ยังไง
ขอบคุณมากครับ
หัวข้อ: Re: สอบถามคำสั่ง if ซ้อนหลายชั้นหน่อยครับ
เริ่มหัวข้อโดย: woravet ที่ มิถุนายน 27, 2014, 13:21:36 PM
select year(vstdate) year,month(vstdate) month
,date(date_format(vstdate,'%Y-%m-01')) firstday
,last_day(vstdate) lastday
,if(month(vstdate) in (10,11,12),1
,if(month(vstdate) in(1,2,3),2
,if(month(vstdate) in(4,5,6),3
,if(month(vstdate) in(7,8,9),4,0)))) TriMonth
from ovst
where vstdate between '2012-10-1' and '2013-9-30'
group by month
order by year,month
หัวข้อ: Re: สอบถามคำสั่ง if ซ้อนหลายชั้นหน่อยครับ
เริ่มหัวข้อโดย: yansue ที่ มิถุนายน 27, 2014, 15:53:36 PM
select year(vstdate) year,month(vstdate) month
,date(date_format(vstdate,'%Y-%m-01')) firstday
,last_day(vstdate) lastday
,if(month(vstdate) in (10,11,12),1
,if(month(vstdate) in(1,2,3),2
,if(month(vstdate) in(4,5,6),3
,if(month(vstdate) in(7,8,9),4,0)))) TriMonth
from ovst
where vstdate between '2012-10-1' and '2013-9-30'
group by month
order by year,month

ได้แล้วครับ ขอบคุณมากเลยครับ