基于SQL Server 2008中的订单数量分配库存数量

时间:2015-04-22 04:32:11

标签: sql-server

我在下面有一个查询,根据订单类型分配库存数量以匹配订单数量。问题是股票数量没有更新。

DECLARE @tblOrder TABLE
    (DealerCode NVARCHAR(50),
     PartCode NVARCHAR(50),
     OrderQty INT,
     OrderType NVARCHAR(50)
    )
INSERT  INTO @tblOrder
        ( DealerCode,
          PartCode,
          OrderQty,
          OrderType )
VALUES  ('D1','A',19,'Urgent'),
('D2','B',10,'Normal'),
('D3','C',11,'HotLine'),
('D1','D',20,'Normal'),
('D2','E',12,'Normal'),
('D1','D',40,'Normal');

DECLARE @tblStock TABLE
    (PartCode NVARCHAR(50),
     StockQty INT)
INSERT  INTO @tblStock
        ( PartCode,
          StockQty)
VALUES  ('A',20),
('B',15),
('C',9),
('D',30),
('E',0)

;WITH ordertemp AS (
select ord.dealercode, 
      ord.partcode, 
      ord.orderqty, 
      ord.ordertype,
      RANK() OVER (ORDER BY case ord.ordertype when 'HotLine' then 1 when 'Urgent' then 2 else 3 end, ord.partcode, ord.dealercode) 'StockPriority',
      sto.stockqty 'InitialStock'
from @tblorder ord
left outer join @tblstock sto
on ord.partcode = sto.partcode )
SELECT
    Orders.dealercode,
    Orders.partcode,
    CASE    WHEN Backlog.PriorQty > Orders.InitialStock THEN 0
            ELSE Orders.InitialStock - Backlog.PriorQty END 'Stock',
    Orders.orderqty,
    CASE    WHEN Backlog.PriorQty + Orders.OrderQty < Orders.InitialStock THEN Orders.OrderQty 
            WHEN Backlog.PriorQty > Orders.InitialStock THEN 0
            ELSE Orders.InitialStock - Backlog.PriorQty END 'Allocated',
    Orders.ordertype
FROM 
    ordertemp Orders
    INNER JOIN
    (
    SELECT A.stockpriority, A.partcode, ISNULL(SUM(B.orderqty),0) 'PriorQty'
    from ordertemp A
    LEFT OUTER JOIN ordertemp B
    on A.partcode = B.partcode
    and A.stockpriority > B.stockpriority 
    group by A.stockpriority, A.partcode ) Backlog
    ON Orders.stockpriority = Backlog.stockpriority
ORDER BY Orders.StockPriority

我得到了结果:

dealercode  partcode    stock   orderqty    allocated   ordertype
D3  C   9   11  9   HotLine
D1  A   20  19  19  Urgent
D2  B   15  10  10  Normal
D1  D   30  20  20  Normal
D1  D   30  40  30  Normal
D2  E   0   12  0   Normal

实际上,零件代码'D'的库存(30)在分配20后必须保持10。 好吗?

2 个答案:

答案 0 :(得分:1)

您获得库存30的原因是因为您使用RANK进行优先排序。如果RANK具有与您的条件ORDER BY case ord.ordertype when 'HotLine' then 1 when 'Urgent' then 2 else 3 end, ord.partcode, ord.dealercode相同的库存优先级,ordertemp将为两个订单记录分配相同的编号。如果您检查dealercode partcode orderqty ordertype StockPriority InitialStock D3 C 11 HotLine 1 9 D1 A 19 Urgent 2 20 D2 B 10 Normal 3 15 D1 D 20 Normal 4 30 D1 D 40 Normal 4 30 D2 E 12 Normal 6 0 的输出,您会看到

StockPriority

请注意,对于第4行和第5行,partcode D的{​​{1}}为4

您可以使用ROW_NUMBER()代替RANK()来解决此问题。您还可以在orderqty中添加ROW_NUMBER(),以指定在较小/较大订单具有相同ordertype, partcodedealercode时是否需要更高优先级。

使用ROW_NUMBER()

查询

DECLARE @tblOrder TABLE
    (DealerCode NVARCHAR(50),
     PartCode NVARCHAR(50),
     OrderQty INT,
     OrderType NVARCHAR(50)
    )
INSERT  INTO @tblOrder
        ( DealerCode,
          PartCode,
          OrderQty,
          OrderType )
VALUES  ('D1','A',19,'Urgent'),
('D2','B',10,'Normal'),
('D3','C',11,'HotLine'),
('D1','D',20,'Normal'),
('D2','E',12,'Normal'),
('D1','D',40,'Normal');

DECLARE @tblStock TABLE
    (PartCode NVARCHAR(50),
     StockQty INT)
INSERT  INTO @tblStock
        ( PartCode,
          StockQty)
VALUES  ('A',20),
('B',15),
('C',9),
('D',30),
('E',0)

;WITH ordertemp AS (
select ord.dealercode, 
      ord.partcode, 
      ord.orderqty, 
      ord.ordertype,
      ROW_NUMBER() OVER (ORDER BY case ord.ordertype when 'HotLine' then 1 when 'Urgent' then 2 else 3 end, ord.partcode, ord.dealercode) 'StockPriority',
      sto.stockqty 'InitialStock'
from @tblorder ord
left outer join @tblstock sto
on ord.partcode = sto.partcode )

SELECT
    Orders.dealercode,
    Orders.partcode,
    CASE    WHEN Backlog.PriorQty > Orders.InitialStock THEN 0
            ELSE Orders.InitialStock - Backlog.PriorQty END 'Stock',
    Orders.orderqty,
    CASE    WHEN Backlog.PriorQty + Orders.OrderQty < Orders.InitialStock THEN Orders.OrderQty 
            WHEN Backlog.PriorQty > Orders.InitialStock THEN 0
            ELSE Orders.InitialStock - Backlog.PriorQty END 'Allocated',
    Orders.ordertype
FROM 
    ordertemp Orders
    INNER JOIN
    (
    SELECT A.stockpriority, A.partcode, ISNULL(SUM(B.orderqty),0) 'PriorQty'
    from ordertemp A
    LEFT OUTER JOIN ordertemp B
    on A.partcode = B.partcode
    and A.stockpriority > B.stockpriority 
    group by A.stockpriority, A.partcode ) Backlog
    ON Orders.stockpriority = Backlog.stockpriority
ORDER BY Orders.StockPriority

答案 1 :(得分:0)

他非常正确 - 当你第一次发布时我假设你每个部分/优先级只有一个条目,而不是重复:)

请注意,如果您添加了orderquantity,如果每个条目的数量相同,则仍会遇到问题。