hi,
I need some help in writing a query . Scenario like i have some items stored in one place. The items are stored as boxes and each box have quantity. When i sell some quantity i need to chnage status like how many boxes are issued. Sales not happed based on box so half quantity in one box also can be issued.
The table details are below:
I have 3 tables named 'Store', 'Box', and 'StoreBox'. The 'Store' table contains the Items and the quantity available in the Store. 'Box' table contains box wise quantity of items. The last table 'StoreBox' contains which box is stored in which store ie a link table for both first and second tables
Table Structure
----------------------
Store
StoreID ItemID Quantity Status
1 1 100 Assign
2 1 50 Assign
3 2 5 Assign
Box
BoxID ItemID RceivedQuantity DispatchedQuantity Status
1 1 25 0 Allotted
2 1 50 0 Allotted
3 1 25 0 Allotted
StoreBox
StoreBoxID StoreID BoxID Status
1 1 1 Assign
2 1 2 Assign
3 1 3 Assign
The data is like this. So total 100 in store 1 for item1.
If i pick 100 quantity from Store1
-> I need to chnage all status of StoreBox as 'UnAssign' . Also chnage chnage status of Box table as Dispatched and updated dispatchedQuatity
If i pick 50 quantity from Store 1
-> I need to chnage StoreBoxID 1 as 'UnAssign' as the quantity is 25 also chnage boxid 1 as 'Dispatched'
-> Then No Change in StoreBoxID 2 as the quantity is partially taken. But update dispatchedQuantity in Box as 25 and status as 'PartiallyDispatched'
Likewise FIFO base i need to update the status. Any possibility to write this as CTE otherwise i need to check with cursor