43
« เมื่อ: ตุลาคม 17, 2011, 16:16:03 PM »
จะทำรายงานโดยให้สรุปค่าใช้จ่ายของแต่ละคน แยกตามหมวด 16 หมวดนะครับ จะเขียนแบบไหนถึงจะให้มัน sum แต่ละหมวดให้นะครับ ผมใช้แบบนี้มันไม่แยกนะครับ ขอบคุณครับ
SELECT o.hn,o.vstdate,CONCAT(p.pname,'',p.fname,' ',p.lname)as ptname,t.`name` as pttypename
,(if(i.group2="1",sum(o.sum_price),null)) as s_1
,(if(i.group2="2",sum(o.sum_price),null)) as s_2
,(if(i.group2="3",sum(o.sum_price),null)) as s_3
,(if(i.group2="4",sum(o.sum_price),null)) as s_4
,(if(i.group2="5",sum(o.sum_price),null)) as s_5
,(if(i.group2="6",sum(o.sum_price),null)) as s_6
,(if(i.group2="7",sum(o.sum_price),null)) as s_7
,(if(i.group2="8",sum(o.sum_price),null)) as s_8
,(if(i.group2="9",sum(o.sum_price),null)) as s_9
,(if(i.group2="10",sum(o.sum_price),null)) as s_10
,(if(i.group2="11",sum(o.sum_price),null)) as s_11
,(if(i.group2="12",sum(o.sum_price),null)) as s_12
,(if(i.group2="13",sum(o.sum_price),null)) as s_13
,(if(i.group2="14",sum(o.sum_price),null)) as s_14
,(if(i.group2="15",sum(o.sum_price),null)) as s_15
,(if(i.group2="16",sum(o.sum_price),null)) as s_16
,SUM(o.sum_price) as total_price
FROM opitemrece o
LEFT JOIN patient p ON p.hn=o.hn
LEFT JOIN pttype t ON t.pttype=o.pttype
LEFT JOIN income i ON i.income=o.income
WHERE o.vstdate BETWEEN '2011-10-01' AND '2011-10-01'
GROUP BY o.vn
ORDER BY o.vstdate,o.hn