I have an assignment to make an inventory report only based on the transactional data table.
For example, I have the table like this, where ProductType determines whether the stock is going in or out (+ for stock in and - for out)
| ProductID | Quantity | ProductType | Date |
| ------------ | ------------ | ------------ | ------------ |
| 1 | 630 | 1 | 2021-10-11 |
| 1 | 630 | -1 | 2021-10-11 |
| 1 | 630 | 1 | 2021-10-12 |
| 1 | 630 | 1 | 2021-10-12 |
| 1 | 430 | -1 | 2021-10-13 |
And based on the data I want to make it look like this based on the parameter inputted which is productid, beginning date, and end date. Assuming the beginning quantity is 0.
ProductID | Date | Beginning Quantity | Quantity In | Quantity Out | Ending Quantity |
---|---|---|---|---|---|
1 | 2021-10-11 | 0 | 630 | 0 | 630 |
1 | 2021-10-11 | 630 | 0 | 630 | 0 |
1 | 2021-10-12 | 630 | 630 | 0 | 630 |
1 | 2021-10-12 | 630 | 630 | 0 | 1260 |
1 | 2021-10-13 | 1260 | 0 | 430 | 830 |
Any help will be much appreciated, thank you!
1条答案
按热度按时间pn9klfpd1#
One example of how to do this. If you have transaction ID then use it instead of
row_number()
because it is just a way how to split the amounts within one day.In addition, recursive CTE or
CASE
statements also should work.If you do not have transaction ID, think about the another representation of the result set. Maybe it is better to show only one row for each day. It should be easier to organise. Something like this:
| ProductID | Date | Beginning Quantity | Quantity In | Quantity Out | Ending Quantity |
| ------------ | ------------ | ------------ | ------------ | ------------ | ------------ |
| 1 | 2021-10-11 | 0 | 630 | 630 | 0 |
| 1 | 2021-10-12 | 0 | 1260 | 0 | 1260 |
| 1 | 2021-10-13 | 1260 | 0 | 430 | 830 |
By the way, there is an error in this line. Beginning Quantity should be zero.