SQL Server:高级更新查询

时间:2017-03-21 16:03:56

标签: sql sql-server sql-server-2008 tsql

我需要一些关于sql更新查询的帮助来计算估计的每日库存消耗,

首先,我填写日期要求表。

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

  • for 21/03/2017 stock = 12-3 = 9

  • for 22/03/2017 stock = 9-1 = 8
  • for 23/03/2017 stock = 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的问题尚未计算,并在该查询中给出错误的结果。

预期结果:

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

提前谢谢

1 个答案:

答案 0 :(得分:2)

在提供获取“订单点”的方式后,使用sum() over()作为运行总计:

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 |
+------------+-------+