FIFO Example Query in SQL Server
This SQL tutorial includes FIFO example query in SQL Server. SQL FIFO method example query is based on two database tables SalesOrder and ProductionOrder sql tables. SQL Server SalesOrder table stores sales orders for produced products coming from customers. To provide ordered products, internal production orders are created in the system. These production orders are stored in ProductionOrder database table.
When a production order is inserted in database table, according to the FIFO method the first sales order created for the corresponding product is fullfiled partially or totally. This SQL tutorial provides a Transact-SQL query to return remaining product quantity for a sales order according to FIFO (First In First Out) basis. FIFO solution in this SQL Server includes CTE (Common Table Expression) queries.
Here is the SQL script required to create database tables SalesOrder and ProductionOrder in SQL Server. Also the SQL script generate sample data in these two database tables.
Here is the sample data
The following CTE query returns the list of sales orders and open quantity whose order quantity is not covered by production orders. The WHERE clause where "LeftQty > 0" is applied filters the orders with open quantities.
The first SQL CTE query s returns the sales orders and an additional cumulative order quantity column. The cumulative order quantity column SoldUpToNow shows total number of orders given for that product up to that order date.
The second SQL CTE query p returns the total number of production orders for each product.
The main SELECT statement which joins two CTE expressions returns the sales order, cumulative order amount, and open order quantity based on FIFO method
This SQL query solution can also be applied to stock movements in a warehouse or in inventory management. Sample data tables in this FIFO example problem, can be replaced with stock movements of a range of products into the warehouse and out from warehouse. Transfer orders can be used as transactions for stock movements.