I have table in MS Access with columns Year, Period (values just 1
and 2
), Costs_Per_Capita and CALCULATED_Period_Avg_Costs, PK is ID.
I need to calculate CALCULATED_Period_Avg_Costs
. It should return average of Costs_Per_Capita
for given period from Period
column. In Excel I do this with SUMIF/COUNTIF which returns Average IF.
Do you have some advice how to write a code in SQL to do that?
Results should look like:
Costs_Per_Capita Period CALCULATED_Period_Avg_Costs
15,505 1 15976.27582
16,368 1 15976.27582
16,037 1 15976.27582
15,995 1 15976.27582
15,000 2 16000
17,000 2 16000
I used statement:
SELECT
Costs_Per_Capita, Period
IFF (Period = 1,
(Select AVG(Costs_Per_Capita) From Costs Where Period = 1),
(Select AVG(Costs_Per_Capita) From Costs Where Period = 2)
AS result
FROM Costs;
Still gets "syntax error (missing operator) in a query expression ..."