คุยกะลุงหนวดคุยไปคุยมาเลยได้เรื่อง sql เผื่อใครจะเอาไปเป็นเป้าหมายในการทำงาน ของ JHCIS ครับ
-------------------------------------------------------------------------------------------------------------
select house.villcode,village.villname as ชื่อหมู่บ้าน
#,pid,birth,getAgeYearNum(birth,current_date) as age,sex
,count(case when getAgeYearNum(birth,current_date) in ('0','1') and person.sex ='1' then 1 else null end) as "man0-1"
,count(case when getAgeYearNum(birth,current_date) in ('0','1') and person.sex ='2' then 1 else null end) as "weman0-1"
,count(case when getAgeYearNum(birth,current_date) in ('0','1') and person.sex IN ('1','2') then 1 else null end) as "sum0-1" #อายุแรกเกิด - 1ปี
,count(case when getAgeYearNum(birth,current_date) in ('0','1','2','3','4','5') and person.sex ='1' then 1 else null end) as "man0-5"
,count(case when getAgeYearNum(birth,current_date) in ('0','1','2','3','4','5') and person.sex ='2' then 1 else null end) as "weman0-5"
,count(case when getAgeYearNum(birth,current_date) in ('0','1','2','3','4','5') and person.sex IN ('1','2') then 1 else null end) as "sum0-5" #อายุแรกเกิด - 1ปี
,count(case when getAgeYearNum(birth,current_date) BETWEEN 6 AND 12 and person.sex ='1' then 1 else null end) as "man6-12"
,count(case when getAgeYearNum(birth,current_date) BETWEEN 6 AND 12 and person.sex ='2' then 1 else null end) as "weman6-12"
,count(case when getAgeYearNum(birth,current_date) BETWEEN 6 AND 12 and person.sex IN ('1','2') then 1 else null end) as "sum6-12"#อายุ 6 - 12ปี
,count(case when getAgeYearNum(birth,current_date) BETWEEN 13 AND 59 and person.sex ='1' then 1 else null end) as "man13-59"
,count(case when getAgeYearNum(birth,current_date) BETWEEN 13 AND 59 and person.sex ='2' then 1 else null end) as "weman13-59"
,count(case when getAgeYearNum(birth,current_date) BETWEEN 13 AND 59 and person.sex IN ('1','2') then 1 else null end) as "sum13-59" #อายุ 13 - 59ปี
,count(case when getAgeYearNum(birth,current_date) >= 60 and person.sex ='1' then 1 else null end) as "man60up"
,count(case when getAgeYearNum(birth,current_date) >= 60 and person.sex ='2' then 1 else null end) as "weman60up"
,count(case when getAgeYearNum(birth,current_date) >= 60 and person.sex IN ('1','2') then 1 else null end) as "sum60up" # อายู 60 ปีขึ้นไป
,count(case when getAgeYearNum(birth,current_date) >= 0 and person.sex ='1' then 1 else null end) as "Totel_man"
,count(case when getAgeYearNum(birth,current_date) >= 0 and person.sex ='2' then 1 else null end) as "Totel_weman"
,count(case when getAgeYearNum(birth,current_date) >= 0 and person.sex IN ('1','2') then 1 else null end) as "Totelsum" #ทุกคนในเขตรับผิดชอบ
#,count(person.pid) as Totle #รวมประชากร
FROM house INNER JOIN person ON person.hcode = house.hcode
INNER JOIN village ON village.villcode = house.villcode
WHERE birth is not null and person.mumoi not in('','0') AND village.villno <> 0 # คัดเลือกเฉพาะหมู่บ้านในเขตบริการ
AND ((person.dischargetype is null) OR (person.dischargetype != '1')) # คัดคนตายออกไป
GROUP BY house.villcode,village.villname
# ORDER BY house.villcode #ยังขาดความสมบูร์คือ ยอดรวม UNION
union
select '','รวม' as ชื่อหมู่บ้าน
#,pid,birth,getAgeYearNum(birth,current_date) as age,sex
,count(case when getAgeYearNum(birth,current_date) in ('0','1') and person.sex ='1' then 1 else null end) as "man0-1"
,count(case when getAgeYearNum(birth,current_date) in ('0','1') and person.sex ='2' then 1 else null end) as "weman0-1"
,count(case when getAgeYearNum(birth,current_date) in ('0','1') and person.sex IN ('1','2') then 1 else null end) as "sum0-1" #อายุแรกเกิด - 1ปี
,count(case when getAgeYearNum(birth,current_date) in ('0','1','2','3','4','5') and person.sex ='1' then 1 else null end) as "man0-5"
,count(case when getAgeYearNum(birth,current_date) in ('0','1','2','3','4','5') and person.sex ='2' then 1 else null end) as "weman0-5"
,count(case when getAgeYearNum(birth,current_date) in ('0','1','2','3','4','5') and person.sex IN ('1','2') then 1 else null end) as "sum0-5" #อายุแรกเกิด - 1ปี
,count(case when getAgeYearNum(birth,current_date) BETWEEN 6 AND 12 and person.sex ='1' then 1 else null end) as "man6-12"
,count(case when getAgeYearNum(birth,current_date) BETWEEN 6 AND 12 and person.sex ='2' then 1 else null end) as "weman6-12"
,count(case when getAgeYearNum(birth,current_date) BETWEEN 6 AND 12 and person.sex IN ('1','2') then 1 else null end) as "sum6-12"#อายุ 6 - 12ปี
,count(case when getAgeYearNum(birth,current_date) BETWEEN 13 AND 59 and person.sex ='1' then 1 else null end) as "man13-59"
,count(case when getAgeYearNum(birth,current_date) BETWEEN 13 AND 59 and person.sex ='2' then 1 else null end) as "weman13-59"
,count(case when getAgeYearNum(birth,current_date) BETWEEN 13 AND 59 and person.sex IN ('1','2') then 1 else null end) as "sum13-59" #อายุ 13 - 59ปี
,count(case when getAgeYearNum(birth,current_date) >= 60 and person.sex ='1' then 1 else null end) as "man60up"
,count(case when getAgeYearNum(birth,current_date) >= 60 and person.sex ='2' then 1 else null end) as "weman60up"
,count(case when getAgeYearNum(birth,current_date) >= 60 and person.sex IN ('1','2') then 1 else null end) as "sum60up" # อายู 60 ปีขึ้นไป
,count(case when getAgeYearNum(birth,current_date) >= 0 and person.sex ='1' then 1 else null end) as "Totel_man"
,count(case when getAgeYearNum(birth,current_date) >= 0 and person.sex ='2' then 1 else null end) as "Totel_weman"
,count(case when getAgeYearNum(birth,current_date) >= 0 and person.sex IN ('1','2') then 1 else null end) as "Totelsum" #ทุกคนในเขตรับผิดชอบ
#,count(person.pid) as Totle #รวมประชากร
FROM house INNER JOIN person ON person.hcode = house.hcode
INNER JOIN village ON village.villcode = house.villcode
WHERE birth is not null and person.mumoi not in('','0') AND village.villno <> 0 # คัดเลือกเฉพาะหมู่บ้านในเขตบริการ
AND ((person.dischargetype is null) OR (person.dischargetype != '1')) # คัดคนตายออกไป
-------------------------------------------------------------------------------------------
แสดงบทความที่มีป้ายกำกับ database แสดงบทความทั้งหมด
แสดงบทความที่มีป้ายกำกับ database แสดงบทความทั้งหมด
นำเข้าข้อมูล Excel สู่ PostgreSQL
การนำข้อมูล จาก EXCEL เข้าสู่ ฐานข้อมูล PostgreSQL
คราวนี้ เป็น ฐานข้อมูลธรรมดา ไม่ใช่ Spatial Database
ขั้นตอนมีดังนี้
คราวนี้ เป็น ฐานข้อมูลธรรมดา ไม่ใช่ Spatial Database
ขั้นตอนมีดังนี้
- แปลงไฟล์ EXCEL หรือ CALC (openoffice) ให้อยู่ในรูป ของ CSV ไฟล์เสียก่อน
- สร้าง Table ใน Postgresql ให้มีจำนวน column เท่ากับ จำนวนข้อมูลที่มี
- ติดต่อฐานข้อมูล ด้วย pgmyAdminIII เพื่อต่อกับ Table ที่ต้องการนำเข้า พึงระวังเรื่อง type ของข้อมูล
- ใช้ SQL นำเข้า ดังตัวอย่างนี้ copy temple from ‘C:/temp/temple.csv’ with delimiter ‘,’ csv header
- ทดสอบว่าข้อมูลเข้าอยู่ครบ ไม่ตกหล่น เป็นอันเรียบร้อย
ที่เหลือ ก็สามารถ ใช้งานได้ เช่นฐานข้อมูลทั่วไป
ที่มา:http://enumap.wordpress.com/category/map-server/
ป้ายกำกับ:
database
สมัครสมาชิก:
บทความ (Atom)




