如何汇总每个项目的“数量”列的运行总计,并仅获取总计为零的日期

问题描述 投票:0回答:1
  Posting Date  Item No_    Quantity    TOTAL   DAYS
  30/06/2016    100920        20         20  
  03/07/2016    100920        -3         17  
  04/07/2016    100920        -2         15  
  05/07/2016    100920        -3         12  
  06/07/2016    100920        -1         11  
  07/07/2016    100920        -1         10  
  08/07/2016    100920        -1          9  
  09/07/2016    100920        -4          5  
  10/07/2016    100920        -1          4  
  11/07/2016    100920        -1          3  
  13/07/2016    100920        -1          2  
  17/07/2016    100920        -2          0     4
  21/07/2016    100920        40         40  
  21/07/2016    100921        40         40  
  22/07/2016    100921       -4          36  
  23/07/2016    100921       -3          33  
  25/07/2016    100921       -3          30  
  26/07/2016    100921       -2          28  
  27/07/2016    100921       -2          26  
  29/07/2016    100921       -3          23  
  30/07/2016    100921       -5          18  
  31/07/2016    100921       -4          14  
  01/08/2016    100921      -10           4  
  02/08/2016    100921       -3           1  
  06/08/2016    100921       -1           0     1
  07/08/2016    100921       20          20  

我需要逐项汇总数量的运行总和,并仅返回总列中库存为零的天数。我还需要获取从零开始到第二天收到订单之间的时间差

我需要输出为:

  Posting Date  Item No_    TOTAL   DAYS
   17/07/2016   100920        0      4
   06/08/2016   100921        0      1
sql
1个回答
1
投票

我认为您想计算totaldays列,尽管您的问题尚不清楚。如果是这样:

select t.*, (next_posting_date - posting_date) as days
from (select t.*,
             sum(quantity) over (partition by item_no order by posting_date) as total,
             lead(posting_date) over (partition by item_no order by posting_date) as next_posting_date
      from t
     ) t
where total = 0;

日期函数众所周知是依赖数据库的,因此days的确切语法可能取决于您的数据库。

© www.soinside.com 2019 - 2024. All rights reserved.