I have a table of stock
ID | Qty | DatePurchased | Price |
---|---|---|---|
11202 | 4 | 01/01/2023 | 3 |
11202 | 3 | 12/06/2023 | 7 |
5050 | 4 | 11/10/2023 | 60 |
11202 | 4 | 12/10/2023 | 5 |
5050 | 8 | 12/12/2023 | 70 |
and Item sold table
ID | Qty |
---|---|
11202 | 6 |
I want to implement the FIFO concept where I can find the total price as follows
I have Quantity purchased in [stock table] on different dates. And I have quantity sold in table [item sold]
let us say I sold 6 quantities for a specific item in my case item "11202" I want to find the total price bymultiplyingy (quantity purchased * price ) // for the first order then multiply the next
4 * 3 = 12 (first order in date 01/01/2023) remaining stock 0
2 * 7 = 14 (second order in date 12/06/2023) remaining stock 1
12 + 14 = 26 total price
I have joined tables which look something like this
joined table
but I don’t know how to add a new column that will include remaining items and the total price
2
Answers
If you want the the total price of each entry:
If you want the total earned money for items:
assuming your quantity always go down, if you want the latest entry’s quantity you should use a subquery.
You should substitute the code you used to join the table with
joined_table
One approach is to create intervals using the cumulative sums of stock and sales, and then join based on the overlaps:
Note: I added the
Dt
column to sales so there was something to order by for the cumulative sum. Presumably you have a unique id that could be used here.Here’s a db<>fiddle.