FYI... The FIFO problem was the subject of an SSC competition some years ago. Many interesting techniques to take into consideration: The ‘FIFO Stock Inventory’ SQL Problem
"I cant stress enough the importance of switching from a sequential files mindset to set-based thinking. After you make the switch, you can spend your time tuning and optimizing your queries instead of maintaining lengthy, poor-performing code."-- Itzik Ben-Gan 2001