select px.icode,d.name
,sum(if(length(px.vn)>9,px.qty,0)) 'OPD_Qty'
,sum(if(length(px.vn)>9,px.sum_price,0)) 'OPD_Price'
,sum(if(length(px.vn)>9,0,px.qty)) 'IPD_Qty'
,sum(if(length(px.vn)>9,0,px.sum_price)) 'IPD_Price'
from opitemrece px
left join drugitems d on px.icode=d.icode
where px.vstdate between '2011-1-1' and '2011-1-1'
and d.name is not null
group by px.icode
order by px.icode