2017-03-21 16 views
1

推定日次在庫量を計算するSQLの更新クエリについてのヘルプが必要ですSQL Server:高度な更新クエリ

最初に、1日の必要条件を満たしたテーブルを作成します。

declare @stock TABLE ([StockDate] [datetime] NULL, 
         [stock] [int]) 

insert into @stock 
values ('2017/21/03', -3), ('2017/22/03', -1), 
     ('2017/23/03', -5), ('2017/24/03', 0), 
     ('2017/25/03', -4), ('2017/26/03', -7), 
     ('2017/27/03', 0); 
  • 初期在庫:12
  • 注文量:10
  • 注文ポイント:3

  • ため21/03/2017ストック= 12-3 = 9

  • 22/03/2017在庫= 9-1 = 8
  • 23/03/2017在庫= 8-5 = 3 < = OrderPoint(3 ) 次いでストック= 8-5 + 10 = 13

私が試み:

update s 
set s.stock = case 
       when s.StockDate = '2017/21/03' 
        then 12 
        else s2.stock 
       end + s.stock 
from @stock s 
left join @stock s2 on s2.StockDate = DATEADD(day, -1, s.StockDate) 

実際の結果:

Date     Stock 
2017-03-21 00:00:00.000 9 
2017-03-22 00:00:00.000 -4 
2017-03-23 00:00:00.000 -6 
2017-03-24 00:00:00.000 -5 
2017-03-25 00:00:00.000 -4 
2017-03-26 00:00:00.000 -11 
2017-03-27 00:00:00.000 -7 

まだ算出していない日-1 problemeとそのクエリで間違った結果を返します。

期待される結果:


Date     Stock 
2017-03-21 00:00:00.000 9 
2017-03-22 00:00:00.000 8 
2017-03-23 00:00:00.000 13 
2017-03-24 00:00:00.000 13 
2017-03-25 00:00:00.000 9 
2017-03-26 00:00:00.000 12 
2017-03-27 00:00:00.000 12 

"注文ポイントを" 調達する方法を提供した後、実行中の合計sum() over()を使用して、事前に

+3

[XKCD PSAのISO 8601](HTTPSを://xkcd.com/1179/) – SqlZim

答えて

2

をありがとう:

declare @orderpoint table ([StockDate] [datetime] NULL, [stock] [int]); 
insert into @orderpoint values 
('20170323',10) 
,('20170326',10); 

select * 
    , RunningTotal = sum(stock) over (order by stockdate) 
from (
    select s.stockdate, stock = s.stock+isnull(o.stock,'') 
    from @stock s 
     left join @orderpoint o 
     on s.stockdate = o.stockdate 
    ) t 
order by stockdate 

rextester デモhttp://rextester.com/GHHU68033

リターン:

+------------+-------+--------------+ 
| stockdate | stock | RunningTotal | 
+------------+-------+--------------+ 
| 2017-03-20 | 12 |   12 | 
| 2017-03-21 | -3 |   9 | 
| 2017-03-22 | -1 |   8 | 
| 2017-03-23 |  5 |   13 | 
| 2017-03-24 |  0 |   13 | 
| 2017-03-25 | -4 |   9 | 
| 2017-03-26 |  3 |   12 | 
| 2017-03-27 |  0 |   12 | 
+------------+-------+--------------+ 

そしてupdateのために、あなたはcommon table expression

;with cte as (
    select 
     stockdate = convert(varchar(10),stockdate,120) 
    , stock 
    , RunningTotal = sum(stock) over (order by stockdate) 
    from (
     select s.stockdate, stock = s.stock+isnull(o.stock,'') 
     from @stock s 
     left join @orderpoint o 
      on s.stockdate = o.stockdate 
    ) t 
) 
update s 
    set stock = cte.stock 
from @stock s 
    inner join cte 
    on s.stockdate = cte.stockdate; 

select 
    stockdate = convert(varchar(10),stockdate,120) 
    , stock 
from @stock; 

リターンを使用することができます。

+------------+-------+ 
| stockdate | stock | 
+------------+-------+ 
| 2017-03-20 | 12 | 
| 2017-03-21 |  9 | 
| 2017-03-22 |  8 | 
| 2017-03-23 | 13 | 
| 2017-03-24 | 13 | 
| 2017-03-25 |  9 | 
| 2017-03-26 | 12 | 
| 2017-03-27 | 12 | 
+------------+-------+ 
+0

ありがとうございます。それはまさに私が望むものです。 –

+0

@UndertakerBatista幸いです! – SqlZim