I'd like to have a bit of confirmation from you guys:
I have a view that has (among others) columns time_in, time_out, Company, weight, that shows when company put some weight into depot and when (if at all) took it out. So it looks like this:
time_in time_out company weight
1-07-2013 3-07-2013 A 10
2-07-2013 NULL A 15
2-07-2013 4-07-2013 B 5
3-07-2013 NULL B 1
etc
Now I'd like to make a balance for each day for each company, so it would be something like:
Date Company Balance
1-07-2013 A 10
1-07-2013 B 0
2-07-2013 A 25
2-07-2013 B 5
3-07-2013 A 15
3-07-2013 B 6
4-07-2013 A 15
4-07-2013 B 1
etc
While I know how to run a basic running total, i.e.
SELECT a.time_IN, a.weight, (SELECT sum (b.weight) from DEPOT b
where b.time_in <= a.time_in) as total_weight from DEPOT a
and I can guess that balance would be something like
SELECT a.time_IN, a.weight, ((SELECT sum (b.weight)
from DEPOT b where b.time_in <= a.time_in)
-(SELECT sum (c.weight) from DEPOT c
where a.time_out >= c.time_out) as balance from DEPOT a
Am I heading in right direction here? Also,do I have to simply add GROUP BY company to get info by company?