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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Cornelia YoderArtistCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
MySQL Server

From novice to tech pro — start learning today.