stored procedures are not my bag and i have done only very Little with mySQL.
But maybe the following gets you started even though it is MSSQl Syntax:
its untested so i am not sure if it really does what is asked.Code:declare @dtOldest as datetime set @dtOldest= (select min(dt) from tbl) while datediff(hour,@dtOldest,getdate())>=4 begin declare @dtNewOldest as datetime=dateadd(minute,1,@dtOldest) INSERT INTO tblNew SELECT min(dt),avg(Value1),avg(Value2) FROM tbl WHERE dt<=@dtNewOldest DELETE FROM tbl WHERE dt<=@dtNewOldest SET @dtOldest=@dtNewOldest end




Reply With Quote