query telemed
ฐานเก่า (50 = telemed NCD, 53=telemed ศว.)
SELECTt.full_name AS 'address',COUNT(DISTINCT a.visit_code) as 'visit'FROM hos_pt_visit aINNER JOIN hos_pt c ON c.hn = a.hnINNER JOIN hos_code_thaiaddress t ON CONCAT(c.province,c.amphur,c.tambon) = t.addressid WHEREa.doctor_room = '53'and c.province = '40'and c.amphur = '07'-- AND a.date_visit BETWEEN '2024-10-01' AND '2025-09-30'AND a.date_visit BETWEEN '2024-10-01' AND '2025-02-30' GROUP BY t.addressid
ฐานใหม่
SELECT t1.name AS 'จังหวัด', t2.name AS 'อำเภอ', t3.name AS 'ตำบล', COUNT(DISTINCT a.hn) AS 'จำนวนผู้ป่วย',COUNT(DISTINCT a.vn) AS 'จำนวน visit'FROM ovst a-- INNER JOIN ovstdiag dx ON dx.vn = a.vnINNER JOIN patient pt ON pt.hn = a.hnLEFT JOIN thaiaddress t1 ON t1.chwpart = pt.chwpart AND t1.amppart = '00' AND t1.tmbpart = '00'LEFT JOIN thaiaddress t2 ON t2.chwpart = pt.chwpart AND t2.amppart = pt.amppart AND t2.tmbpart = '00'LEFT JOIN thaiaddress t3 ON t3.chwpart = pt.chwpart AND t3.amppart = pt.amppart AND t3.tmbpart = pt.tmbpartWHERE a.vstdate BETWEEN '2025-02-01' AND '2025-09-30' AND a.main_dep = '053' AND t1.name = 'ขอนแก่น' AND t2.name = 'น้ำพอง' AND t1.name IS NOT NULLGROUP BY t1.name, t2.name, t3.nameORDER BY จำนวนผู้ป่วย DESC
query Home ward แยกตำบล
ฐานเก่า
(SELECTt.full_name AS 'ที่อยู่',COUNT(*) AS 'จำนวน'FROMipd aINNER JOIN pat b ON b.an = a.anINNER JOIN hos_pt c ON c.hn = b.hnINNER JOIN hos_code_thaiaddress t ON CONCAT(c.province,c.amphur,c.tambon) = t.addressidWHEREb.ward = '20'AND t.addressid = '400701')UNION(SELECTt.full_name AS 'ที่อยู่',COUNT(*) AS 'จำนวน'FROMipd aINNER JOIN pat b ON b.an = a.anINNER JOIN hos_pt c ON c.hn = b.hnINNER JOIN hos_code_thaiaddress t ON CONCAT(c.province,c.amphur,c.tambon) = t.addressidWHEREb.ward = '20'AND t.addressid = '400702')UNION(SELECTt.full_name AS 'ที่อยู่',COUNT(*) AS 'จำนวน'FROMipd aINNER JOIN pat b ON b.an = a.anINNER JOIN hos_pt c ON c.hn = b.hnINNER JOIN hos_code_thaiaddress t ON CONCAT(c.province,c.amphur,c.tambon) = t.addressidWHEREb.ward = '20'AND t.addressid = '400703')UNION(SELECTt.full_name AS 'ที่อยู่',COUNT(*) AS 'จำนวน'FROMipd aINNER JOIN pat b ON b.an = a.anINNER JOIN hos_pt c ON c.hn = b.hnINNER JOIN hos_code_thaiaddress t ON CONCAT(c.province,c.amphur,c.tambon) = t.addressidWHEREb.ward = '20'AND t.addressid = '400704')UNION(SELECTt.full_name AS 'ที่อยู่',COUNT(*) AS 'จำนวน'FROMipd aINNER JOIN pat b ON b.an = a.anINNER JOIN hos_pt c ON c.hn = b.hnINNER JOIN hos_code_thaiaddress t ON CONCAT(c.province,c.amphur,c.tambon) = t.addressidWHEREb.ward = '20'AND t.addressid = '400705')UNION(SELECTt.full_name AS 'ที่อยู่',COUNT(*) AS 'จำนวน'FROMipd aINNER JOIN pat b ON b.an = a.anINNER JOIN hos_pt c ON c.hn = b.hnINNER JOIN hos_code_thaiaddress t ON CONCAT(c.province,c.amphur,c.tambon) = t.addressidWHEREb.ward = '20'AND t.addressid = '400706')UNION(SELECTt.full_name AS 'ที่อยู่',COUNT(*) AS 'จำนวน'FROMipd aINNER JOIN pat b ON b.an = a.anINNER JOIN hos_pt c ON c.hn = b.hnINNER JOIN hos_code_thaiaddress t ON CONCAT(c.province,c.amphur,c.tambon) = t.addressidWHEREb.ward = '20'AND t.addressid = '400707')UNION(SELECTt.full_name AS 'ที่อยู่',COUNT(*) AS 'จำนวน'FROMipd aINNER JOIN pat b ON b.an = a.anINNER JOIN hos_pt c ON c.hn = b.hnINNER JOIN hos_code_thaiaddress t ON CONCAT(c.province,c.amphur,c.tambon) = t.addressidWHEREb.ward = '20'AND t.addressid = '400708')UNION(SELECTt.full_name AS 'ที่อยู่',COUNT(*) AS 'จำนวน'FROMipd aINNER JOIN pat b ON b.an = a.anINNER JOIN hos_pt c ON c.hn = b.hnINNER JOIN hos_code_thaiaddress t ON CONCAT(c.province,c.amphur,c.tambon) = t.addressidWHEREb.ward = '20'AND t.addressid = '400709')UNION(SELECTt.full_name AS 'ที่อยู่',COUNT(*) AS 'จำนวน'FROMipd aINNER JOIN pat b ON b.an = a.anINNER JOIN hos_pt c ON c.hn = b.hnINNER JOIN hos_code_thaiaddress t ON CONCAT(c.province,c.amphur,c.tambon) = t.addressidWHEREb.ward = '20'AND t.addressid = '400710')UNION(SELECTt.full_name AS 'ที่อยู่',COUNT(*) AS 'จำนวน'FROMipd aINNER JOIN pat b ON b.an = a.anINNER JOIN hos_pt c ON c.hn = b.hnINNER JOIN hos_code_thaiaddress t ON CONCAT(c.province,c.amphur,c.tambon) = t.addressidWHEREb.ward = '20'AND t.addressid = '400711')UNION(SELECTt.full_name AS 'ที่อยู่',COUNT(*) AS 'จำนวน'FROMipd aINNER JOIN pat b ON b.an = a.anINNER JOIN hos_pt c ON c.hn = b.hnINNER JOIN hos_code_thaiaddress t ON CONCAT(c.province,c.amphur,c.tambon) = t.addressidWHEREb.ward = '20'AND t.addressid = '400712')