SQL比较并从第二个表中按值拆分

时间:2018-08-01 12:07:59

标签: sql sql-server-2016

我面临挑战,我不知道该如何解决。

在TA中(表A),我有一些订单行,需要预定可用库存。但是保留库存需要使用FIFO(先进先出)。 TB(表B)显示了可用库存。

查询的结果必须类似于RESULT。

TA

ID  |   ITEM    |   QUANTITY|    
1   |   X1234   |   4       |    
2   |   X1234   |   6       |   
3   |   C3456   |   10      |    
4   |   C3456   |   15      |

TB

ID  |   ITEM    |   QUANTITY|    
A   |   X1234   |   6       |    
B   |   X1234   |   1000    |    
C   |   X1234   |   500     |    
D   |   C3456   |   40      |       
E   |   C3456   |   100     |

结果

ID(TA)  |   ITEM(TA)    | QUANTITY(TA)  |   ID(TB)  |   QuantityFromTB 
1       |   X1234       |   4           |   A       |   4    
2       |   X1234       |   6           |   A       |   2    
2       |   X1234       |   6           |   B       |   4    
3       |   C3456       |   10          |   D       |   10    
4       |   C3456       |   15          |   D       |   15

更新:

下面的查询可以解决问题,但是如果您有更好的解决方案,那就是welkom

DECLARE @orderlines table
(
    rowID int,
    ItemCode nvarchar(50),
    Quantity int
)

DECLARE @blanketlines table
(
    rowID int,
    DocNum nvarchar(50),
    LineNum int,
    ItemCode nvarchar(50),
    Quantity int
)

DECLARE @result table
(
    UUID nvarchar(36),
    orderlinesID int,
    blanketlinesID int,
    DocNum nvarchar(50),
    LineNum int,
    ItemCode nvarchar(50),
    Quantity int
)

INSERT INTO @orderlines 
SELECT 1, 'X1234', 6
UNION
SELECT 2, 'X1234', 4


INSERT INTO @blanketlines SELECT 
ROW_NUMBER() OVER(ORDER BY ORDR.DocDate ASC) AS [rowID],
ORDR.DocNum, RDR1.LineNum, RDR1.ItemCode, RDR1.Quantity FROM RDR1
INNER JOIN ORDR ON ORDR.DocEntry = RDR1.DocEntry
WHERE ORDR.DocCur = 'USD' AND ORDR.DocStatus = 'O' AND ORDR.U_DBS_ORDERTYPE = 
'B' AND RDR1.LineStatus = 'O' AND RDR1.ItemCode = 'X1234'
ORDER BY ORDR.DocDate


DECLARE @I int
DECLARE @RECORD_COUNT int = 0
DECLARE @WHILE_COUNT int = 0

DECLARE @rowID int
DECLARE @ItemCode NVARCHAR(50)
DECLARE @Quantity int
DECLARE @UUID NVARCHAR(36)

SELECT @WHILE_COUNT = COUNT(*) FROM @orderlines WHERE Quantity > 0  

WHILE @WHILE_COUNT > 0
BEGIN 

SELECT TOP 1 @rowID = rowID, @ItemCode = ItemCode, @Quantity = Quantity FROM @orderlines WHERE Quantity > 0 ORDER BY rowID ASC

SELECT @UUID = newID()

INSERT INTO @result 
    SELECT TOP 1 
        @UUID AS UUID,
        @rowID AS orderlinesID,
        rowID AS blanketlinesID, 
        DocNum, 
        LineNum, 
        ItemCode, 
        CASE WHEN Quantity - @Quantity >= 0 THEN @Quantity ELSE Quantity END AS Quantity  
    FROM @blanketlines 
    WHERE ItemCode = @ItemCode AND Quantity > 0
    ORDER BY rowID ASC                 

 UPDATE @orderlines SET Quantity = Quantity - (SELECT Quantity FROM @result WHERE UUID = @UUID) FROM @orderlines WHERE rowID = @rowID
 UPDATE @blanketlines SET Quantity = Quantity - (SELECT Quantity FROM @result WHERE UUID = @UUID) FROM @blanketlines WHERE rowID = (Select blanketlinesID FROM @result WHERE UUID = @UUID)              

SELECT @WHILE_COUNT = COUNT(*) FROM @orderlines WHERE Quantity > 0   
END

SELECT * FROM @result

0 个答案:

没有答案