select id, country_name, max(avg_temp) as max_avg, month
from dimstation as s
join
(select (avg(range_temp)) as avg_temp, station, month
from dimtime as t
join
(select time_id, station_id as station, (value_max - value_min) as range_temp
from
(select time_id, station_id, value as value_max
from
factobservation as o
join dimelement as e on o.element_id = e.id
where name = "TMAX") as tbl1
join
(select time_id as time_id_min, station_id as station_id_min, value as value_min
from
factobservation as o
join dimelement as e on o.element_id = e.id
where name = "TMIN") as tbl2
on tbl1.station_id = tbl2.station_id_min and tbl1.time_id = tbl2.time_id_min) as temp
on temp.time_id = t.date_id
group by month, station) as tblavg
on s.id = tblavg.station
group by id, month;