我遇到问题,希望获得帮助或想法或任何搜索关键字,可以帮助我找到解决以下问题的方法: 我有2个表,1个包含导入信息,1个包含导出信息。
tblImport (iCode, ComponentCode, iTotal)
tblExport (eCode, ProductCode, ComponentCode, eNum)
Idie allocation quantity two tables
要求:我必须创建一个包含eCode的表,该eCode具有每个componentCode的eNum 当sum(eNum)= iTotal时。 (请看图片)
第3列是我想要的结果,在该列(5)= iTotal - (sum(eNum)over(iCode分区,eCode))。条件是每个集合(iCode,eCode,ComponentCode)只出现一次。这意味着我分配的行将被忽略。 这里的问题是,如果tblImport通过ComponentCode加入tblExport,那么这将不是真的,会有重复的行。
我可以在SQL中实现吗?如果是这样,请在技术上让我知道这样做。 如果没有,请指出我如何在SQL或C#中实现它。 来自谷歌翻译 谢谢大家。
答案 0 :(得分:-1)
我做到了!
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!