set @nstock:=3000;
set @nuse:=0;
select o.vn,o.hn,o.an,p.pname,p.fname,p.lname
,o.rxdate,o.rxtime
,o.icode,concat(d.name,' ',d.strength) drugname
,@nstock 'stock',o.qty,@nstock:=@nstock-o.qty 'rest'
,@nuse:=@nuse+o.qty 'Use'
from opitemrece o
left join patient p on o.hn=p.hn
left join drugitems d on o.icode=d.icode
where o.rxdate between '2012-1-1' and '2012-1-31'
and d.name regexp 'Acetyl'
order by o.rxdate,o.rxtime