Inventory Valuation in ERP: FIFO vs Weighted Average Cost with Oracle SQL Examples
How ERP software values stock: FIFO and weighted average cost explained with a worked example, Oracle SQL to value FIFO layers, a PL/SQL moving-average update, returns, landed costs and how to reconcile stock value with the general ledger.
ERP · Inventory
Inventory Valuation in ERP: FIFO vs Weighted Average Cost
Ask an ERP user “how much stock do we have?” and you get a quantity. Ask the finance team the same question and they want a value – and the value depends on which costing method the system uses. When the stock report and the balance sheet disagree, the cause is almost always one of three things: the costing method is not applied consistently, some transactions do not carry a cost, or the stock ledger and the general ledger are posted from different sources.
This guide explains the two methods most ERP systems use – FIFO and weighted average – with one worked example, then shows how to implement each in Oracle with SQL and PL/SQL. It is written for developers and implementers; the accounting choice itself should be made with your accountant according to the reporting standards that apply to your business.
1. One example, two methods
An item has these movements in a month, starting with zero stock:
| Date | Movement | Qty | Unit cost |
|---|---|---|---|
| 1st | Purchase | 100 | 10.00 |
| 8th | Purchase | 50 | 12.00 |
| 15th | Sale (issue) | 120 | ? |
| 22nd | Purchase | 80 | 11.00 |
FIFO – first in, first out
Issues consume the oldest receipts first. The 120 units sold on the 15th come from the 100 units at 10.00 and 20 units at 12.00:
- Cost of goods sold = 100 × 10 + 20 × 12 = 1,240
- Closing stock = 30 × 12 + 80 × 11 = 1,240 (110 units)
Weighted average (moving average)
Each receipt recalculates the average cost of everything in stock; issues are costed at the current average:
- After 1st: 100 units, average 10.00
- After 8th: (1,000 + 600) ÷ 150 = 10.6667
- Issue on 15th: 120 × 10.6667 = 1,280; 30 units remain, value 320
- After 22nd: (320 + 880) ÷ 110 = 10.9091; closing stock 110 units, value 1,200
Same quantities, same purchases – but cost of sales differs by 40 and closing stock by 40. Neither is wrong; they are different methods. What is wrong is mixing them, or changing method in the middle of a year without a proper adjustment.
2. The stock ledger: every movement carries quantity and value
Whichever method you choose, the foundation is a stock ledger where every movement – purchase, sale, return, transfer, adjustment, production – is one row with quantity and value:
CREATE TABLE inv_txn (
txn_id NUMBER GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
item_id NUMBER NOT NULL,
wh_id NUMBER NOT NULL,
txn_date DATE NOT NULL,
txn_type VARCHAR2(10) NOT NULL, -- GRN, SALE, RET_IN, RET_OUT, ADJ, XFER
qty NUMBER NOT NULL, -- + in, - out
unit_cost NUMBER(18,4),
value NUMBER(18,2), -- qty * unit_cost, signed
ref_doc VARCHAR2(30),
posted_gl CHAR(1) DEFAULT 'N'
);
Stock quantity is SUM(qty) and stock value is SUM(value). If any issue row has an empty cost, your value report and your accounts will drift apart from that day on.
3. Implementing FIFO in Oracle SQL
With FIFO, the remaining stock is always made of the most recent receipts. Given the receipts and the total quantity issued, a running total tells you how much of each receipt layer is still in stock:
WITH rcpt AS (
SELECT txn_id, txn_date, qty, unit_cost,
SUM(qty) OVER (ORDER BY txn_date, txn_id) AS cum_qty
FROM inv_txn
WHERE item_id = :p_item
AND wh_id = :p_wh
AND qty > 0
),
issued AS (
SELECT NVL(-SUM(qty), 0) AS total_out
FROM inv_txn
WHERE item_id = :p_item
AND wh_id = :p_wh
AND qty < 0
)
SELECT r.txn_date, r.unit_cost,
LEAST(r.qty, GREATEST(0, r.cum_qty - i.total_out)) AS qty_left,
LEAST(r.qty, GREATEST(0, r.cum_qty - i.total_out)) * r.unit_cost AS value_left
FROM rcpt r CROSS JOIN issued i
ORDER BY r.txn_date, r.txn_id;
On the example above this returns 0 units left from the 1st, 30 from the 8th and 80 from the 22nd – a value of 1,240. Summing value_left gives the FIFO closing value at any time.
To cost each issue at posting time, the same logic runs in PL/SQL: before posting an issue of N units, find the oldest layers that still have quantity and take N units from them, writing the resulting cost on the issue row. For high-volume items it is common to maintain a separate layers table (receipt id, quantity remaining) that is reduced by each issue, instead of recalculating from all history.
4. Implementing weighted average in PL/SQL
Weighted average is simpler to run: keep the current quantity and average cost per item and warehouse, and update it on each receipt.
CREATE TABLE inv_item_cost (
item_id NUMBER NOT NULL,
wh_id NUMBER NOT NULL,
qty NUMBER DEFAULT 0,
avg_cost NUMBER(18,6) DEFAULT 0,
CONSTRAINT inv_item_cost_pk PRIMARY KEY (item_id, wh_id)
);
CREATE OR REPLACE PROCEDURE inv_post_receipt (
p_item IN NUMBER, p_wh IN NUMBER, p_date IN DATE,
p_qty IN NUMBER, p_cost IN NUMBER, p_ref IN VARCHAR2
) IS
l_qty inv_item_cost.qty%TYPE;
l_avg inv_item_cost.avg_cost%TYPE;
BEGIN
SELECT qty, avg_cost INTO l_qty, l_avg
FROM inv_item_cost
WHERE item_id = p_item AND wh_id = p_wh
FOR UPDATE; -- serialise postings
IF l_qty + p_qty > 0 THEN
l_avg := (GREATEST(l_qty, 0) * l_avg + p_qty * p_cost)
/ (GREATEST(l_qty, 0) + p_qty);
END IF;
UPDATE inv_item_cost
SET qty = l_qty + p_qty, avg_cost = l_avg
WHERE item_id = p_item AND wh_id = p_wh;
INSERT INTO inv_txn (item_id, wh_id, txn_date, txn_type,
qty, unit_cost, value, ref_doc)
VALUES (p_item, p_wh, p_date, 'GRN',
p_qty, p_cost, ROUND(p_qty * p_cost, 2), p_ref);
END;
Issues read avg_cost at the moment of posting and write it on the issue row. Three details matter in practice:
- Lock the cost row (
FOR UPDATE) so two users posting at the same time cannot both read the old average. - Back-dated transactions change every average after them. Either block back-dating into closed periods, or run a recalculation job that re-costs the item from that date forward.
- Negative stock breaks averages. If your business sells before the purchase is entered (common in busy shops), decide how the system costs those issues and correct them when the receipt arrives.
5. Returns, transfers and landed cost
- Sales returns should come back at the cost at which they were issued, not at today’s cost – link the return to the original invoice line.
- Purchase returns leave at the purchase cost of the returned receipt under FIFO, and at the current average under weighted average.
- Warehouse transfers move value as well as quantity; the receiving warehouse takes the cost of the sending one.
- Landed costs – freight, customs duty, clearing charges – increase the cost of the goods they relate to. Allocate them to receipt lines (by value, quantity or weight) when the bills arrive, and re-cost the affected stock.
6. Reconcile stock value with the general ledger every month
The stock account in the general ledger must equal the stock value from the inventory ledger at month end. Post inventory journals from inv_txn (for example, a daily summary per item group and transaction type) and compare:
SELECT (SELECT SUM(value) FROM inv_txn
WHERE txn_date < :p_month_end + 1) AS stock_ledger_value,
(SELECT SUM(debit - credit) FROM gl_lines
WHERE account_code = :p_stock_account
AND je_date < :p_month_end + 1) AS gl_stock_value
FROM dual;
A difference means a transaction was posted to one side but not the other – usually a manual journal to the stock account, or inventory movements that were never posted. Find and fix it before closing the month; differences only get harder to explain as time passes.
Quick checklist
- One costing method per item, agreed with your accountant, applied consistently
- Every stock movement stores quantity and value
- FIFO layers or moving average maintained at posting time
- Cost rows locked during posting; back-dating controlled
- Returns linked to original documents; landed costs allocated to receipts
- Monthly reconciliation of stock ledger value with the GL stock account
Related: barcode inventory setup for retail, pharmacy inventory and expiry management, and our inventory & distribution software and full ERP demos.
Need inventory that matches your accounts?
AbairTech builds ERP, POS and distribution software on Oracle APEX and PL/SQL, with stock ledgers that reconcile to the GL.
Related implementation services
Need help applying these concepts in a business system? Explore ERP software development or Oracle Database and PL/SQL services.