Skip to content
Advertisement

How can I write a Postgres (SQL) query for FIFO ‘closing stock’ inventory valuation?

Background

I need to implement inventory valuation / costing using the FIFO (first-in, first-out) method.
I’m running Postgres 11 running on CentOS 7. I’ve looked at, and tried, a fair number of hypotheses from SO and the wider internet (as well as searching my own print library which includes SQL Queries for Mere Mortals, PostgreSQL Up & Running, The SQL Cookbook, Practical Issues In Database Management, and other quality reference works), and to date, I can’t find a solution that works for closing inventory valuation.
(I’ve also tried reasoning it out on my own, but have failed to come up with a plausible appraoch)

NOTE In my case, I have permission to change the table structure, etc, of the setup, so I can add / remove / change anything in the setup as needed (such as, e.g., adding a direction column to the movements table, as some approaches I’ve tried have indicated, changing queries, etc etc)

Current setup

I have a table mockup_inv_movements:

and this view mockup_inv_movements_with_fifo_cost adds FIFO cost for sale / ‘out’ rows, calculated from a query (shown later below):

Adding some test inventory movement data:

OK, now here’s the query that calculates the ‘sale/out’ price for each, taken from this question, which seems to work; note that I’m only pulling in the column fifo_unit_cost from this query at the moment:

Now here’s the part where I’m having trouble.
I need to calculate the value of remaining stock / inventory on hand, also known as “closing stock” or “closing inventory.” I’ve tried a number of approaches including this question and this ‘set-based speed phreakery’ method, the latter of which I readily admit that I don’t fully comprehend,

The approach that has come closest to working for me is this older hypothesis from Ranjeet Rana, BUT although it does seem to assign the FIFO costs according to the correct breakdown, the sum of closing stock for each SKU does not seem to match the raw difference between ‘in’ and ‘out’ quantities.

Here’s the closing stock query adapted from Rana (comments mine; I left them in just in case they might indicate where my error is).

With this in place, we should be able to get the sum of closing stock value per SKU with:

However, as I mentioned, the totals do not match up with the basic inventory difference calculation (specifically in this test example, I would expect 75 units of bar_product to be represented in closing stock, whereas this query shows 100):

It seems like this would be the kind of thing that has a more-or-less standardized solution, but so far none of the resources I’ve found / tried has guided me to a working approach.

How can I accurately do FIFO closing stock / inventory valuation in Postgres?
All guidance much appreciated!

Advertisement

Answer

Using “Set-based Speed Phreakery: The FIFO Stock Inventory SQL Problem” as an example, re-working that approach for Postgres and the change of table/columns produces this query:

and from your sample data the result produced is this:

also see: https://dbfiddle.uk/?rdbms=postgres_11&fiddle=f564a6cfda3374c2057b437f845a4bdf

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement