• Maybe cheating slightly, but you could try using the SQLRollingStats library from TotallySQL.com...

    Charles Southey
    www.totallysql.com