Growing Degree Day
SET @gdd_total:=0; SELECT q1.doy, q1.gdd, round(@gdd_total := @gdd_total + q1.gdd) as gdd_tot FROM (SELECT DAYOFYEAR(DATE_FORMAT(`hive_observation_time_local`, '%Y-%m-%d')) AS doy, (least(greatest(max(wx_temp_c),10),30) - least(greatest(min(wx_temp_c),10),30))/2 AS gdd from HIVE_DATA where hive_id=10 group by DATE_FORMAT(`hive_observation_time_local`, '%Y-%m-%d') ) AS q1
SELECT hive_id, DAYOFYEAR(DATE_FORMAT(`hive_observation_time_local`, '%Y-%m-%d')) AS doy, (least(greatest(max(wx_temp_c),10),30) - least(greatest(min(wx_temp_c),10),30))/2 AS gdd from HIVE_DATA group by DATE_FORMAT(`hive_observation_time_local`, '%Y-%m-%d'), hive_id