This is going to look complicated, but bear with me. It needs some clarification on what is meant by others/rate however the principle is sound. If you have a primary key on financies that you can use then a more elegant (GROUP BY ... ROLLUP) solution may be viable however I've not sufficient experience with that to offer reliable advice. Here goes how I would address the issue.
Long-winded option
( SELECT financesTallied.date, financesTallied.rate, financesTallied.supply_fee, financesTallied.demand_fee, financesTallied.charged_fee, financesTallied.total_costs, financesTallied.net_return FROM ( SELECT financeWithNetReturn.*, @supplyFee := @supplyFee + financeWithNetReturn.supply_fee, @demandFee := @demandFee + financeWithNetReturn.demand_fee, @charedFee := @charedFee + financeWithNetReturn.charged_fee FROM ( // Calculate net return based off total costs SELECT financeData.*, financeData.supply_fee - financeData.total_costs AS net_return FROM ( // Select the data SELECT date, rate, supply_fee, demand_fee, charged_fee, (supply_fee+demand_fee+charged_fee)/rate AS total_costs // need clarification on others/rate FROM financies WHERE date BETWEEN '2010-01-10' AND '2011-01-01' ORDER BY date ASC ) AS financeData ) AS financeWithNetReturn, ( SELECT @supplyFee := 0 @demandFee := 0 @charedFee := 0 ) AS variableInit ) AS financesTallied ) UNION ( SELECT '*Total*', NULL, @supplyFee, @demandFee, @chargedFee, NULL, NULL )
Working from the innermost query to the outermost. This query selects the basic fees and calculates the total_costs for this row. This total_costs formula will need adjustment as I'm not 100% clear on what you were looking for there. Will refer to this as [SQ1]
SELECT date, rate, supply_fee, demand_fee, charged_fee, (supply_fee+demand_fee+charged_fee)/rate AS total_costs // need clarification on others/rate FROM financies WHERE date BETWEEN '2010-01-10' AND '2011-01-01' ORDER BY date ASC
Next level up I'm just reusing the calculated total_costs column with the supply_fee column to add in a net_return column. This concludes the basic data you need per-row, will refer to this as [SQL2]
SELECT financeData.*, financeData.supply_fee - financeData.total_costs AS net_return FROM ([SQ1]) AS financeData
At this level it's time to start tallying up the values, so need to initialise the variables required with 0 values ([SQL3])
SELECT @supplyFee := 0 @demandFee := 0 @charedFee := 0
Next level up, I'm using the calculated rows to calculate the totals ([SQL4])
SELECT financeWithNetReturn.*, @supplyFee := @supplyFee + financeWithNetReturn.supply_fee, @demandFee := @demandFee + financeWithNetReturn.demand_fee, @charedFee := @charedFee + financeWithNetReturn.charged_fee FROM ([SQL2]) AS financeWithNetReturn, ([SQL3]) AS variableInit
Now finally at the top level, just need to output the desired columns without the calculated columns ([SQL5])
SELECT financesTallied.date, financesTallied.rate, financesTallied.supply_fee, financesTallied.demand_fee, financesTallied.charged_fee, financesTallied.total_costs, financesTallied.net_return FROM ([SQL4]) AS financesTallied
And then output it UNIONED with a totals row
([SQL5]) UNION ( SELECT '*Total*', NULL, @supplyFee, @demandFee, @chargedFee, NULL, NULL )
as net returnsand'2010-01-10 AND' '2011-01-01'don't look right, and you open two parenthesis while only closing one...