Please look at the following scenario.
Assume I have a transaction table with the following data
Date Type Item Qty Purchase Price
2016-01-01 Buy ABC 10 500
2016-01-10 Buy ABC 2 90
2016-08-01 Sell ABC 12 650
2016-09-05 Buy ABC 13 600
I want to get the avg purchase price of item ABC for current in stock items using a sql query.
So the answer in end should be something like this
Item Qty Purchase Price PricePerItem
ABC 13 600 46.15
In this example it will only be for the last row (row 4) since all other bought stock was sold.
But not all items will always be sold, so I need to make sure to keep the purchase price of all unsold items in mind.
Items work on first in first out basis.
Is this even possible to do in a single query?
Assume I have a transaction table with the following data
Date Type Item Qty Purchase Price
2016-01-01 Buy ABC 10 500
2016-01-10 Buy ABC 2 90
2016-08-01 Sell ABC 12 650
2016-09-05 Buy ABC 13 600
I want to get the avg purchase price of item ABC for current in stock items using a sql query.
So the answer in end should be something like this
Item Qty Purchase Price PricePerItem
ABC 13 600 46.15
In this example it will only be for the last row (row 4) since all other bought stock was sold.
But not all items will always be sold, so I need to make sure to keep the purchase price of all unsold items in mind.
Items work on first in first out basis.
Is this even possible to do in a single query?