select o.icode,concat(d.name,' ',d.strength) drug,d.units
,sum(o.qty) 'Total'
,sum(o.sum_price) 'Sum_Price'
,sum(if(o.vn is not null,o.qty,0)) 'OPD'
,sum(if(o.vn is not null,o.sum_price,0)) 'OPD_Price'
,sum(if(o.an is not null,o.qty,0)) 'IPD'
,sum(if(o.an is not null,o.sum_price,0)) 'IPD_Price'
from opitemrece o
left join drugitems d on d.icode=o.icode
where o.rxdate between '2013-1-1' and '2013-1-31'
and d.name is not null
group by o.icode
order by d.name