how to write a simple stored procedure

I have a MySql database.  I would like to write a stored procedure to do the following:

From a tableX with the following columns:

period  (datetime fromat)   price (decimal format)      result (decimal format)
2008-01-20 09:25:00            1                                      NULL
2008-01-20 09:26:00            3                                      NULL
2008-01-20 09:27:00            5                                      NULL
2008-01-20 09:28:00            7                                      NULL
2008-01-20 09:29:00            1                                      NULL
2008-01-20 09:30:00            6                                      NULL

For every row, I would like to sum up the last 1000 prices and subtract from them the sum of the 1000 prices before them and put that difference in the column 'result'.  For the purposes of an example, lets assume that instead of looking at the last 1000 prices I am looking at the last two.  So in this case the last row would have (6+1)-(7+5) or -5, the second to last row would have (1+7)-(5+3) or 0...etc.  The 1st three rows which dont have enough data to make the calculation should be left as NULLs.

In advance, thanks a lot.



Server info:
MySQL 5.0.45-community-nt via TCP/IP
MySQL Client Version 5.1.11
InnoDB tables
jmokrauerAsked:
Who is Participating?
 
Cornelia YoderConnect With a Mentor ArtistCommented:
This is a non-trivial project.  Here is a place to start learning:

http://www.databasejournal.com/features/mysql/article.php/3525581

and a book on how to program stored procedures:

http://www.oreilly.com/catalog/mysqlspp/
0
All Courses

From novice to tech pro — start learning today.