私はそれをしました!
CREATE TABLE Orders
([OrderID] int, [Item] varchar(3), [Quantity] int)
;
INSERT INTO Orders
([OrderID], [Item], [Quantity])
VALUES
(1, 'pen', 80),
(2, 'pen', 30),
(3, 'pen', 25)
;
CREATE TABLE Inventory
([InvID] int, [Lot] varchar(3), [Item] varchar(3), [Quantity] int)
;
INSERT INTO Inventory
([InvID], [Lot], [Item], [Quantity])
VALUES
(1, '001', 'pen', 100),
(2, '002', 'pen', 20),
(3, '003', 'pen', 30)
;
INSERT INTO dbo.Inventory VALUES (4, '001','Ink',50)
INSERT INTO dbo.Inventory VALUES (5, '002','Ink',50)
INSERT INTO Orders VALUES (4, 'Ink', 30)
INSERT INTO Orders VALUES (5, 'Ink', 60)
WITH CTE_Orders AS
(
SELECT *, ROW_NUMBER() OVER (PARTITION BY Item ORDER BY OrderID) AS RN
FROM dbo.Orders
)
, CTE_Inventory AS
(
SELECT *, ROW_NUMBER() OVER (PARTITION BY Item ORDER BY InvID) AS RN
FROM dbo.Inventory
)
, CTE AS
(
SELECT o.RN AS OrderRN,
inv.RN AS InvRN,
OrderID ,
o.Item ,
o.Quantity AS OrderedQuantity ,
InvID ,
Lot ,
inv.Quantity AS InvQuantity,
CASE WHEN inv.Quantity - o.Quantity > 0 THEN o.Quantity ELSE inv.Quantity END AS ServedQuantity ,
CASE WHEN inv.Quantity - o.Quantity > 0 THEN 0 ELSE o.Quantity - inv.Quantity END AS LeftToServe,
CASE WHEN inv.Quantity - o.Quantity > 0 THEN inv.Quantity - o.Quantity ELSE 0 END AS LeftInLot
FROM CTE_Orders o
INNER JOIN CTE_Inventory inv ON o.Item = inv.Item
--WHERE OrderID = 1 AND InvID = 1
WHERE o.RN =1 AND inv.RN = 1
UNION ALL
SELECT CASE WHEN c1.LeftInLot <=0 THEN c1.OrderRN ELSE c2.OrderRN END AS OrderRN
,CASE WHEN c1.LeftInLot <=0 THEN c2.InvRN ELSE c1.InvRN END AS InvRN
,CASE WHEN c1.LeftInLot <=0 THEN c1.OrderID ELSE c2.OrderID END AS OrderID
,CASE WHEN c1.LeftInLot <=0 THEN c1.Item ELSE c2.Item END AS Item
,CASE WHEN c1.LeftInLot <=0 THEN c1.OrderedQuantity ELSE c2.OrderedQuantity END AS OrderedQuantity
,CASE WHEN c1.LeftInLot <=0 THEN c2.InvID ELSE c1.InvID END AS InvID
,CASE WHEN c1.LeftInLot <=0 THEN c2.Lot ELSE c1.Lot END AS Lot
,CASE WHEN c1.LeftInLot <=0 THEN c2.InvQuantity ELSE c1.LeftInLot END AS InvQuantity
,CASE WHEN CASE WHEN c1.LeftInLot <=0 THEN c2.InvQuantity ELSE c1.LeftInLot END - CASE WHEN c1.LeftInLot <=0 THEN c1.LeftToServe ELSE c2.OrderedQuantity END > 0
THEN CASE WHEN c1.LeftInLot <=0 THEN c1.LeftToServe ELSE c2.OrderedQuantity END
ELSE CASE WHEN c1.LeftInLot <=0 THEN c2.InvQuantity ELSE c1.LeftInLot END
END AS ServedQuantity
,CASE WHEN CASE WHEN c1.LeftInLot <=0 THEN c2.InvQuantity ELSE c1.LeftInLot END - CASE WHEN c1.LeftInLot <=0 THEN c1.LeftToServe ELSE c2.OrderedQuantity END > 0
THEN 0
ELSE CASE WHEN c1.LeftInLot <=0 THEN c1.LeftToServe ELSE c2.OrderedQuantity END - CASE WHEN c1.LeftInLot <=0 THEN c2.InvQuantity ELSE c1.LeftInLot END
END AS LeftToServe
,CASE WHEN CASE WHEN c1.LeftInLot <=0 THEN c2.InvQuantity ELSE c1.LeftInLot END - CASE WHEN c1.LeftInLot <=0 THEN c1.LeftToServe ELSE c2.OrderedQuantity END > 0
THEN CASE WHEN c1.LeftInLot <=0 THEN c2.InvQuantity ELSE c1.LeftInLot END - CASE WHEN c1.LeftInLot <=0 THEN c1.LeftToServe ELSE c2.OrderedQuantity END
ELSE 0
END AS LeftInLot
FROM CTE c1
INNER JOIN
(
SELECT o2.RN AS OrderRN,
inv2.RN AS InvRN,
InvID ,
Lot ,
inv2.Item ,
inv2.Quantity AS InvQuantity,
OrderID ,
o2.Quantity AS OrderedQuantity
FROM
CTE_Inventory inv2
INNER JOIN CTE_Orders o2 ON inv2.Item = o2.Item
) c2
ON c1.Item = c2.Item AND
((c2.InvRN = c1.InvRN + 1 AND c2.OrderRN = c1.OrderRN AND c1.LeftInLot <= 0) OR (c2.OrderRN = c1.OrderRN + 1 AND c2.InvRN = c1.InvRN AND c1.LeftInLot>0))
)
SELECT * FROM CTE
ORDER BY item,OrderID
これは機能します。
Tks!
これは後でサイトにアクセスするすべての人にとって有益な答えではありません。ただのリンクを超えた情報はありません。あなたの答え*にコード*を表示し、有用な説明を加えてください。私はC#タグも削除したいと思います。これはC#と実際には関係がありません。 –
私の答えにsqlコードを追加することはできません –
ええと、なぜですか?それは絶対にうまくいくはずです。 –