I\'ved got a problem same as this but I am using Postgres.
Calculate balance with mysql
have a table which contains the following data:
ID
select t.*, sum("In"-"Out") over(order by id) as balance
from tbl t
order by id
Fiddle: http://sqlfiddle.com/#!15/97dc5/2/0
Consider changing your column names "In" / "Out" so that you don't need to put them in quotes. (They are reserved words)
If you wanted only one customer (customer_id = 2):
select t.*, sum("In"-"Out") over(order by id) as balance
from tbl t
where customer_id = 2
order by id
If your query were to span multiple customers and you wanted a running balance that RESTARTED with each customer, you could use:
select t.*, sum("In"-"Out") over( partition by customer_id
order by customer_id, id ) as balance_by_cust
from tbl t
order by customer_id, id