select - Update table Data without using while loop in sql server 2005 -
in sql have table data below
id type amount 1 type1 2000 2 type1 1000 3 type2 500 4 type3 3000 5 type1 2000 6 type2 500 7 type3 5000 8 type1 1000
and want datas in select statement below
id type amount current 1 type1 2000 2000 2 type1 1000 1000 3 type2 500 500 4 type3 3000 3000 5 type1 2000 3000 6 type2 -500 0 7 type3 5000 2000 8 type1 -1000 4000
and on means each type must have current total amount based on amount type , need dont have while loop because takes long time execute
for eg: in type 1 id amount current 1 2000-add 2000 2 1000-sub 1000 3 2000-add 3000 4 1000-add 4000
how it
i think query work:
select id,type,amount,(select sum(amount) mytable t1 t1.type=t2.type , t1.id<=t2.id) currenttotal mytable t2
Comments
Post a Comment