I have the following code to calculate the min,max and average of temperature per month. However I also want to determine the average minimum and average maximum per month.
declare @cnt int = 1
while @cnt < 13
begin
select maand, min(Stasie1) as minStasie1, max(stasie1) as maksStasie1, avg(stasie1) as gemStasie1
from GekombineerAlleMaande
where maand = @cnt
group by maand
select maand,
min(Stasie1) as minStasie1,
max(stasie1) as maksStasie1,
avg(stasie1) as gemStasie1
from GekombineerAlleMaande
where maand between 1 and 13
group by maand
I think that this is what you are after, you need a set of intermediate results that you can then query (for the monthly output) and average out (the overall average of minimums)... the below does it all in one hit
; with temperature as ( -- a dummy table of temperatures
select * from (
values
(1, 10), (1, 15), (1, 8)
, (2, 15), (2, 25), (2, 30)
, (3, 9), (3, 16), (3, 56), (3, 9)
) d(monthnumber, temp)
)
-- what is the average min / max per month
select monthnumber, min(temp) as [min], max(temp) as [max], avg(temp) as [avg]
from temperature
group by monthnumber
union all
-- what is the average of the min / max above
select 13 as dummymonth, avg([min]), avg([max]), avg(d.avg) from (
-- repeat the monthly min/max'ing and then average the result in the outer query
select monthnumber, min(temp) as [min], max(temp) as [max], avg(temp) as [avg]
from temperature
group by monthnumber
) as d
order by monthnumber asc