edit: As User Larna Requested a create/insert for the subquery tables I have added these as well.
The below query returns:
DocNum ItemCode Planned Qty WhsCode BinCode QuantityToPick 901903 A00001 12.000000 98 98-A001 7.000000 901903 A00001 12.000000 98 98-A002 5.000000 --sum of 5 and 7 makes 12 901904 A00001 17.000000 98 98-A001 7.000000 901904 A00001 17.000000 98 98-A002 6.000000 901904 A00001 17.000000 98 98-A003 4.000000 --sum of 7,6 and 4 makes 17 But I want to have this result: DocNum ItemCode Planned Qty WhsCode BinCode QuantityToPick 901903 A00001 12.000000 98 98-A001 7.000000 901903 A00001 12.000000 98 98-A002 5.000000 --sum of 5 and 7 makes 12 -- continue from bin code 98A002 for the new docnum as we had 5 out of 6 where consumed 901904 A00001 17.000000 98 98-A002 1.000000 901904 A00001 17.000000 98 98-A003 12.000000 901904 A00001 17.000000 98 98-A004 4.000000 --sum of 1,12 and 4 makes 17
So basically I want to continue where I left off after using the first DocNum,ItemCode,Quantity to eat
the first bin and the second bin partially
and continue with the second DocNum,ItemCode,Quantity to "eat" remaining quantity of the second bin and then the other left over bins until I reach 0
But I'm a bit stuck on how to do this. Does anyone has any advice/solution?
-- Create Tables for Subquerys + insert relevant data
CREATE TABLE [dbo].[WOR1](-- Removed the unimportant columns
[DocEntry] [int] NOT NULL,
[LineNum] [int] NOT NULL,
[ItemCode] [nvarchar](50) NULL,
[PlannedQty] [numeric](19, 6) NULL,
[IssuedQty] [numeric](19, 6) NULL,
[wareHouse] [nvarchar](8) NULL
)
INSERT INTO [dbo].[WOR1]
VALUES(154 ,0 ,'A00001' ,12.000000 ,0.000000, '01')
,(154 ,1 ,'P10001' ,12.000000 ,0.000000, '01')
,(154 ,2 ,'L10001' ,12.000000 ,0.000000, '01')
,(155 ,0 ,'A00001' ,17.000000 ,0.000000, '01')
,(155 ,1 ,'P10001' ,17.000000 ,0.000000, '01')
,(155 ,2 ,'L10001' ,17.000000 ,0.000000, '01')
CREATE TABLE [dbo].[OWOR](-- Removed the unimportant columns
[DocEntry] [int] NOT NULL,
[DocNum] [int] NOT NULL,
[Status] [char](1) NULL
)
INSERT INTO [dbo].[OWOR]
VALUES( 154, 901903, 'R')
,( 155, 901904, 'R')
CREATE TABLE [dbo].[OIBQ](-- Removed the unimportant columns
[ItemCode] [nvarchar](50) NOT NULL,
[BinAbs] [int] NOT NULL,
[OnHandQty] [numeric](19, 6) NULL,
[WhsCode] [nvarchar](8) NULL
)
INSERT INTO [dbo].[OIBQ]
VALUES('A00001', 7, 15.000000, '98')
,('A00001', 2, 7.000000, '98')
,('A00001', 3, 6.000000, '98')
,('A00001', 4, 12.000000, '98')
CREATE TABLE [dbo].[OBIN](-- Removed the unimportant columns
[AbsEntry] [int] NOT NULL,
[BinCode] [nvarchar](228) NOT NULL,
[WhsCode] [nvarchar](8) NULL
)
INSERT INTO [dbo].[OBIN]
VALUES(1, '98-SYSTEM-BIN-LOCATION', '98')
,(2, '98-A001', '98')
,(3, '98-A002', '98')
,(4, '98-A003', '98')
,(5, '98-A004', '98')
,(6, '98-A005', '98')
,(7, '98-A006', '98')
Query with subqueries:
SELECT
WOPICK.DocNum,
WOPICK.ItemCode,
WOPICK.Quantity as 'Planned Qty',
BINSTOCK.WhsCode,
BINSTOCK.BinCode,
CASE WHEN (BINSTOCK.Quantity -(BINSTOCK.RunningTotalQty - WOPICK.Quantity)) <BINSTOCK.Quantity
THEN (BINSTOCK.Quantity -(BINSTOCK.RunningTotalQty - WOPICK.Quantity))
ELSE BINSTOCK.Quantity END as 'QuantityToPick'-- FinalQty
FROM (
SELECT IBQ.ItemCode, SUM(IBQ.OnHandQty)as 'Quantity',BIN.WhsCode, BIN.BinCode ,
SUM(SUM(OnHandQty)) OVER (PARTITION BY IBQ.ItemCode
ORDER BY IBQ.ItemCode,BIN.BinCode ) as 'RunningTotalQty'
FROM OIBQ IBQ INNER JOIN OBIN BIN on IBQ.BinAbs = BIN.AbsEntry
GROUP BY IBQ.ItemCode, BIN.WhsCode, BIN.BinCode
) BINSTOCK
INNER JOIN (
SELECT distinct WOR.DocNum, WOR1.LineNum, WOR1.ItemCode, WOR1.PlannedQty - WOR1.IssuedQty as 'Quantity'
FROM OWOR WOR
INNER JOIN WOR1 WOR1 on WOR.DocEntry = WOR1.DocEntry
INNER JOIN OIBQ IBQ on IBQ.ItemCode = WOR1.ItemCode
where WOR.Status = 'R'
) WOPICK on BINSTOCK.ItemCode = WOPICK.ItemCode
WHERE BINSTOCK.RunningTotalQty - BINSTOCK.Quantity < WOPICK.Quantity
order by WOPICK.LineNum;
Query without subqueries:
-- SIMPLIFIED Version (Subquery results in tables BINSTOCK & WOPICK)
CREATE TABLE [dbo].[BINSTOCK](
ItemCode NVARCHAR(10) NOT NULL
,Quantity DECIMAL (19,6) NOT NULL
,WhsCode INT NOT NULL
,BinCode NVARCHAR (10) NOT NULL
,RunningTotalQty DECIMAL (19,6) NOT NULL
)
INSERT INTO [dbo].[BINSTOCK]
VALUES('A00001' , 7.000000, 98, '98-A001' , 7.000000),
('A00001' , 6.000000, 98, '98-A002' , 13.000000),
('A00001' , 12.000000, 98, '98-A003' , 25.000000),
('A00001' , 15.000000, 98, '98-A006' , 40.000000)
CREATE TABLE [dbo].[WOPICK](
DocNum INT NOT NULL
,LineNum INT NOT NULL
,ItemCode NVARCHAR (10) NOT NULL
,Quantity DECIMAL (19,6) NOT NULL
)
INSERT INTO [dbo].[WOPICK]
VALUES(901903,0,'A00001',12.000000),
(901904,0,'A00001',17.000000)
SELECT
WOPICK.DocNum,
WOPICK.ItemCode,
WOPICK.Quantity as 'Planned Qty',
BINSTOCK.WhsCode,
BINSTOCK.BinCode,
CASE WHEN (BINSTOCK.Quantity -(BINSTOCK.RunningTotalQty - WOPICK.Quantity)) <BINSTOCK.Quantity
THEN (BINSTOCK.Quantity -(BINSTOCK.RunningTotalQty - WOPICK.Quantity))
ELSE BINSTOCK.Quantity END as 'QuantityToPick'-- FinalQty
FROM BINSTOCK
INNER JOIN WOPICK on BINSTOCK.ItemCode = WOPICK.ItemCode
WHERE BINSTOCK.RunningTotalQty - BINSTOCK.Quantity < WOPICK.Quantity
order by WOPICK.LineNum;