2017-02-27 9 views
1

在庫プールがロットに分かれています。注文が到着し、指定されたプールからその注文を最初のロットから開始します。時には、プール内のすべてのロットよりも大きい注文が入ってきます。ストックを取得するには、別のプールに移動する必要があります。SQL倉庫在庫ピッキング消費量が数量より多い

私はthis very helpful linkがほぼ正確に記述されていますが、答えはデータを表示するために必要なものから1歩です。

サンプル株式プール、たくさん及び量:

+----+------+-----+----------+ 
| Id | Pool | Lot | Quantity | 
+----+------+-----+----------+ 
| 1 | 1 | 1 |  5 | 
| 2 | 1 | 2 |  10 | 
| 3 | 1 | 3 |  4 | 
| 4 | 2 | 1 |  7 | 
| 5 | 3 | 1 |  1 | 
| 6 | 3 | 2 |  5 | 
+----+------+-----+----------+ 

サンプル受注:

+------+-----+----------+------------------+-----------------+-----------------+------------------+ 
| Pool | Lot | Quantity | QuantityConsumed | RunningQuantity | RemainingDemand | SurplusOrDeficit | 
+------+-----+----------+------------------+-----------------+-----------------+------------------+ 
| 1 | 1 |  5 |    17 |    0 |    12 | NULL    | 
| 1 | 2 |  10 |    17 |    0 |    2 | NULL    | 
| 1 | 3 |  4 |    17 |    2 |    0 | 2    | 
| 2 | 1 |  7 |    8 |    0 |    1 | -1    | 
| 3 | 1 |  1 |    6 |    0 |    5 | NULL    | 
| 3 | 2 |  5 |    6 |    0 |    0 | 0    | 
+------+-----+----------+------------------+-----------------+-----------------+------------------+ 

+----+------+------------------+ 
| Id | Pool | QuantityConsumed | 
+----+------+------------------+ 
| 1 | 1 |    17 | 
| 2 | 2 |    8 | 
| 3 | 3 |    6 | 
+----+------+------------------+ 

リンク質問からHABOの答えを使用して、以下の結果を得ますしかし、私はプール2の注文が前のプールに行き、余剰があればそれから取るようにしたいs。

最終募集結果:

+------+-----+----------+------------------+-----------------+-----------------+------------------+ 
| Pool | Lot | Quantity | QuantityConsumed | RunningQuantity | RemainingDemand | SurplusOrDeficit | 
+------+-----+----------+------------------+-----------------+-----------------+------------------+ 
| 1 | 1 |  5 |    17 |    0 |    12 | NULL    | 
| 1 | 2 |  10 |    17 |    0 |    2 | NULL    | 
| 1 | 3 |  4 |    17 |    1 |    0 | 1    | 
| 2 | 1 |  7 |    8 |    0 |    0 | 0    | 
| 3 | 1 |  1 |    6 |    0 |    5 | NULL    | 
| 3 | 2 |  5 |    6 |    0 |    0 | 0    | 
+------+-----+----------+------------------+-----------------+-----------------+------------------+ 

私はそこのカップルは、プール注文数量が利用可能なプールの量と照合される前に、リンクのソリューションに一時テーブルの連鎖球菌も考えていたが、その後、私は知りません追加の在庫が前のプールから取られるように注文を分割する方法。

意見や提案が参考になります。私はSQL Server 2014を使用していますが、システムの移行中にDB2の最新バージョンでもこれを実行したいと考えています。

+0

私は、消費のいずれかの量は、プールでの最大量を上限とされている別のプールに分割しなければならないか、または別の再帰CTEは、実行中の量が量として割り当てられている結果に実行する必要があります考えています赤字は消費された量として割り当てられます – houstonwp

答えて

0

あなたが提供した結合テーブル(whと呼んでいます)を使って直接作業することに決めました。明らかな理由から、私はidという列を追加しました。

Rextester demo:

create table wh (Id int, Pool int, Lot int, Quantity int, QuantityConsumed int, RunningQuantity int, RemainingDemand int, SurplusOrDeficit int); 
GO 
insert into wh values (1,1,1,5,17,0,12,NULL); 
insert into wh values (2,1,2,10,17,0,2,NULL); 
insert into wh values (3,1,3,4,17,2,0,2); 
insert into wh values (4,2,1,7,8,0,1,-1); 
insert into wh values (5,3,1,1,6,0,7,NULL); 
insert into wh values (6,3,2,5,6,0,2,0); 

declare @continue int, @d_id int, @s_id int, @deficit int, @max_surplus int; 
set @continue = 1; 
while @continue = 1 
begin 
    -- Find the first pool with deficit: 
    select @d_id = min(id) from wh where SurplusOrDeficit < 0; 
    if @d_id is null 
    begin 
     -- We are done, all demand has been satisfied 
     set @continue = 0; 
    end 
    else 
    begin 
     select @deficit = -SurplusOrDeficit from wh where id = @d_id; 
     -- Find the first pool (if such exists) to satisfy the remaining demand for id = @d_id: 
     select @s_id = min(id) from wh where SurplusOrDeficit >= @deficit; 
     if @s_id is null 
     begin 
      -- Partially satisfy the remaining demand from a pool which has the biggest surplus: 
      select @max_surplus = max(SurplusOrDeficit) from wh where SurplusOrDeficit > 0; 
      if @max_surplus is not null 
      begin 
       select @s_id = min(id) from wh where SurplusOrDeficit = @max_surplus; 
       update wh set SurplusOrDeficit = 0, RunningQuantity = RunningQuantity - @max_surplus where id = @s_id; 
       update wh set SurplusOrDeficit = SurplusOrDeficit + @max_surplus, RemainingDemand = RemainingDemand - @max_surplus where id = @d_id; 
      end 
      else 
      begin 
       -- We are done, some demand cannot be satisfied 
       set @continue = 0; 
      end 
     end 
     else 
     begin 
      update wh set RunningQuantity = RunningQuantity - @deficit, SurplusOrDeficit = SurplusOrDeficit - @deficit where id = @s_id; 
      update wh set RemainingDemand = 0, SurplusOrDeficit = 0 where id = @d_id; 
     end 
    end 
end 

select * from wh; 
関連する問題