I have the following SQL and want to calculate cumulative totals for the field targ.new_numberofoutputs, grouped by Programme, Programme Contract, Output and Month. Can anybody help?
SELECT targ.new_programmemoidname AS 'Programme',
targ.new_programcontracouttidname AS 'Programme Contract',
targ.new_monthlyoutputsidname AS 'Output',
targ.new_monthnumber AS 'Month',
targ.new_numberofoutputs AS 'Target',
COUNT(act.new_outputevmonthnumber) AS 'Actual',
case when targ.new_numberofoutputs - COUNT(act.new_outputevmonthnumber) < 0 then 0 else
COUNT(act.new_outputevmonthnumber) - targ.new_numberofoutputs end AS 'Difference',
CASE WHEN targ.new_numberofoutputs = 0 THEN 0 ELSE
round(cast(count(act.new_outputevmonthnumber) as float)/targ.new_numberofoutputs * 100, 0) END as '% Achieved',
FROM FilteredNew_monthlyoutput AS targ
LEFT JOIN FilteredNew_evidence AS act
ON targ.new_monthlyprofileoutid = act.new_claimperiodid
AND targ.new_monthlyoutputsid = act.new_evidenceforoutputid
GROUP BY targ.new_programmemoidname,
targ.new_programcontracouttidname,
targ.new_monthlyoutputsidname,
targ.new_monthnumber,
targ.new_numberofoutputs
ORDER BY count(act.new_outputevmonthnumber) desc, targ.new_programmemoidname,
targ.new_programcontracouttidname,
targ.new_monthlyoutputsidname,
targ.new_monthnumber
SELECT targ.new_programmemoidname AS 'Programme',
targ.new_programcontracouttidname AS 'Programme Contract',
targ.new_monthlyoutputsidname AS 'Output',
targ.new_monthnumber AS 'Month',
targ.new_numberofoutputs AS 'Target',
COUNT(act.new_outputevmonthnumber) AS 'Actual',
case when targ.new_numberofoutputs - COUNT(act.new_outputevmonthnumber) < 0 then 0 else
COUNT(act.new_outputevmonthnumber) - targ.new_numberofoutputs end AS 'Difference',
CASE WHEN targ.new_numberofoutputs = 0 THEN 0 ELSE
round(cast(count(act.new_outputevmonthnumber) as float)/targ.new_numberofoutputs * 100, 0) END as '% Achieved',
FROM FilteredNew_monthlyoutput AS targ
LEFT JOIN FilteredNew_evidence AS act
ON targ.new_monthlyprofileoutid = act.new_claimperiodid
AND targ.new_monthlyoutputsid = act.new_evidenceforoutputid
GROUP BY targ.new_programmemoidname,
targ.new_programcontracouttidname,
targ.new_monthlyoutputsidname,
targ.new_monthnumber,
targ.new_numberofoutputs
ORDER BY count(act.new_outputevmonthnumber) desc, targ.new_programmemoidname,
targ.new_programcontracouttidname,
targ.new_monthlyoutputsidname,
targ.new_monthnumber