I have a table where I keep information about financial returns. The columns includes receipts, repairs, service_amount, tyres, fuel, salaries_allowances, others.
I would like to get the cumulative net return like:
Date Receipts repairs service amount Total costs net Return
2012-01-10 0.00 120,000.00 0.00 120,000.00 120,000.00
2012-01-12 60,000.00 0.00 0.00 60,000.00 60,000.00
I am currently using this query:
SELECT
a.consignment_date, a.receipts, a.service_amount, a.repairs, a.tyres,
a.salaries_allowances, a.clearing_fee, a.others,
a.service_amount + a.repairs + a.tyres + a.salaries_allowances + a.clearing_fee + a.others as total_costs,
(b.receipts -(a.service_amount + a.repairs + a.tyres + a.salaries_allowances + a.clearing_fee + a.others)) as netreturn
FROM
vw_local_freight a CROSS JOIN vw_local_freight b
WHERE
a.consignment_date >= b.consignment_date AND a.vehicle_no='123X'
GROUP BY a.consignment_date
I would look at a query like
The subquery tallies up your costs and credits, then the outer query performs your running total. If you need individual column sums within the inner join then they can be added in easily enough.