Tuesday, January 8, 2013

[Script] การหาข้อมูล ใหม่ในปี ในเดือน และในวัน (HOSxP)

การหาข้อมูล ใหม่ในปี ในเดือน และในวัน (HOSxP)


มีเพื่อนๆหลายๆท่าน ถามผมมาว่า หากต้องการ หาข้อมูล Visit  ในปี ในเดือน และในวัน  จะหาตรงไหน และจากตารางไหนบ้าง มาดูการเขียน Script  หา กันครับ คงจะเคยมีการขอข้อมูลประมาณนี้กันมาบ้างแล้วนะครับ ถ้าอยากรู้ว่าต้องเขียนยังไง มาดูกันเลย 
ในที่นี้ ข้อมูลส่วนนี้จะมีอยู่ในตาราง vn_stat (ของ HOSxP ,HOSxP_PCU) จะนำเสนอ โดยการนำ Script ด้านล่าง ไป run ที่ SQL Query 

select hn,vn,vstdate ,count_in_day,count_in_month,count_in_yearfrom vn_stat
where vstdate between '2013-01-08' and '2013-01-08'
order by vn desc     limit 10


จะเห็นข้อมูล ดังภาพ 







จะเห็นข้อมูล  count_in_day         = Visit ในวัน 
                  count_in_month     = Visit ในเดือน 
                  count_in_year        = Visit ในปี
ดังนั้นหาก ต้องการหา ผู้ที่มาครั้งแรกในปี หาได้จาก Script 

select hn,vn,vstdate ,count_in_day,count_in_month,count_in_yearnt_in_day,count_in_month,count_in_yearfrom vn_stat
where vstdate between '2013-01-08' and '2013-01-08'
and count_in_year ='0'
order by vn desc     limit 10



หรือหากต้องการหาผู้ที่มารับบริการมากกว่า 3 ครั้งขึ้นไป หาได้จาก Script

select hn,vn,vstdate ,count_in_day,count_in_month,count_in_yearnt_in_day,count_in_month,count_in_yearfrom vn_stat
where vstdate between '2013-01-08' and '2013-01-08'
and count_in_year >'3'
order by vn desc     limit 10


ก็น่าจะพอเห็นภาพนะครับ ในส่วนอื่น เช่น ในเดือน ในวัน ก็ประยุกต์ใช้ ได้เลยครับ 


No comments: