I am trying to find the average of a count on a field. Can anybody tell me what is wrong with my SQL?
select new_programmemoidname as 'Programme', new_programcontracouttidname as 'Programme Contract',
new_outputtitlemthidname as 'Output Name', new_monthlyprofileoutidname as 'Month',
new_numberofoutputs as 'Target', COUNT(e.new_evidenceid) as 'Actual',
avg(evcount) from
(select COUNT(e.new_evidenceid) evcount
from FilteredNew_monthlyoutput m
left join FilteredNew_evidence e
on m.new_monthlyprofileoutid = e.new_claimperiodid
AND m.new_monthlyoutputsid = e.new_evidenceforoutputid
where m.statecode = 0
and m.new_enddateutc < CURRENT_TIMESTAMP
and new_programmemoidname like 'MCC RSL%'
and new_programcontracouttidname = 'No Programme Contract'
group by new_programmemoidname, new_programcontracouttidname, new_outputtitlemthidname,
new_monthnumber, new_monthlyprofileoutidname, new_numberofoutputs) as 'Average',
COUNT(e.new_evidenceid) - new_numberofoutputs as 'Variance'
from FilteredNew_monthlyoutput m
left join FilteredNew_evidence e
on m.new_monthlyprofileoutid = e.new_claimperiodid
AND m.new_monthlyoutputsid = e.new_evidenceforoutputid
where m.statecode = 0
and m.new_enddateutc < CURRENT_TIMESTAMP
and new_programmemoidname like 'MCC RSL%'
and new_programcontracouttidname = 'No Programme Contract'
select new_programmemoidname as 'Programme', new_programcontracouttidname as 'Programme Contract',
new_outputtitlemthidname as 'Output Name', new_monthlyprofileoutidname as 'Month',
new_numberofoutputs as 'Target', COUNT(e.new_evidenceid) as 'Actual',
avg(evcount) from
(select COUNT(e.new_evidenceid) evcount
from FilteredNew_monthlyoutput m
left join FilteredNew_evidence e
on m.new_monthlyprofileoutid = e.new_claimperiodid
AND m.new_monthlyoutputsid = e.new_evidenceforoutputid
where m.statecode = 0
and m.new_enddateutc < CURRENT_TIMESTAMP
and new_programmemoidname like 'MCC RSL%'
and new_programcontracouttidname = 'No Programme Contract'
group by new_programmemoidname, new_programcontracouttidname, new_outputtitlemthidname,
new_monthnumber, new_monthlyprofileoutidname, new_numberofoutputs) as 'Average',
COUNT(e.new_evidenceid) - new_numberofoutputs as 'Variance'
from FilteredNew_monthlyoutput m
left join FilteredNew_evidence e
on m.new_monthlyprofileoutid = e.new_claimperiodid
AND m.new_monthlyoutputsid = e.new_evidenceforoutputid
where m.statecode = 0
and m.new_enddateutc < CURRENT_TIMESTAMP
and new_programmemoidname like 'MCC RSL%'
and new_programcontracouttidname = 'No Programme Contract'