]ลอง ดูครับ อันนี้เป็นแยกตามสิทธิการรักษาครับ
select v.pttype as pcode,p.name as pttype_name,count(distinct(v.hn))
as hn_count,
sum(v.uinc08) as sum_01,
sum(v.uinc12+v.uinc10) as sum_02,
sum(v.uinc15) as sum_03,
sum(v.uinc07) as sum_001,
sum(v.uinc02) as sum_04,
sum(v.uinc01) as sum_05,
sum(v.uinc04) as sum_06,
sum(v.uinc14+v.uinc06) as sum_09,
sum(v.uinc09) as sum_07,
sum(v.uinc05+v.uinc11) as sum_10,
sum(v.uinc13+v.uinc17) as sum_11,
sum(v.uinc16) as sum_12,
sum(v.uinc01+v.uinc02+v.uinc03+v.uinc04+v.uinc05+v.uinc06+v.uinc07+v.uinc08+v.uinc09+v.uinc10+
v.uinc11+v.uinc12+v.uinc13+v.uinc14+v.uinc15+v.uinc16+v.uinc17) as all_income
from inc_opd_stat v,pttype p
where v.pttype=p.pttype and v.vstdate
between '2010-10-01' and '2010-10-31'
and v.uincome>0
group by v.pttype,p.name
order by pttype_name