Find 运行 Total In sql - 如果第一列没有值则从第二列开始计算

Find Running Total In sql - if 1st column has no value then calculate from 2nd column

declare @tbl as table
(
    ItemId int,
    SOQty int,
    DIQty int ,
    IssueQty int,
    BalanceQty int,
    CreateDate datetime,
    StockQty int,
    WIPQty int
)

insert into @tbl values 
(1,10,10,0,10,'2021-12-16 19:28:32.200',10,0), 
--(2,5,5,1,4,'2021-12-17 19:28:05.200',80),
(1,15,10,10,5,'2021-12-18 19:28:34.200',30, 0),
(1,8,5,2,2,'2021-12-19 19:28:35.200',30,0)
--(2,15,15,0,15,'2021-12-20 19:28:05.200',80),
--(2,12,10,5,5,'2021-12-22 19:28:05.200',80)
--(1,15,10,10,5,'2021-12-18 19:28:34.200',30,0)
 
  
update x 
set x.StockQty = tx.StockQty  
from @tbl x
join 
    (select * 
     from 
         (select 
              *,
              row_number() over (partition by itemid order by CreateDate) as RowNo 
          from @tbl) as t 
     where t.RowNo = 1) as tx on tx.CreateDate = x.CreateDate
 
update x 
set x.StockQty = 0 
from @tbl x
join 
    (select * 
     from 
         (select 
              *,
              row_number() over (partition by itemid order by CreateDate) as RowNo 
          from @tbl) as t 
     where t.RowNo != 1) as tx on tx.CreateDate = x.CreateDate
 

declare @tbl1 as table
(
    ItemId int,
    SOQty int,
    DIQty int ,
    IssueQty int,
    BalanceQty int,
    CreateDate datetime,
    StockQty int,
    WIPQty int,
    StockAllocateQty int,
    UpdatedStockQty int
)
 
insert into @tbl1
    select 
        *, 
        BalanceQty as StockAllocateQty,
        sum(StockQty - BalanceQty) over (partition by ItemId 
                                         order by CreateDate   
                                         rows between unbounded preceding and current row) as UpdatedStockQty  
    from @tbl 
    -- order by CreateDate
 
 declare @tblItemWIPQty table
 (
 ItemId int,
 WIPQty  int
 )

 insert into @tblItemWIPQty values(1,40)
 
 
 
update x set x.WIPQty =  tt.WIPQty from @tbl1 x
join 
(select * from  
(
select top 1 * from @tbl1 where UpdatedStockQty < 0
) as t) as t on t.CreateDate = x.CreateDate 
join @tblItemWIPQty tt on tt.ItemId = x.ItemId
 

 
select *,BalanceQty as AllocateQtyWIP ,SUM(case when StockQty - BalanceQty >= 0 then StockQty -BalanceQty else WIPQty - BalanceQty end) 
over(partition by ItemId order by CreateDate   Rows BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) as UpdatedStockQtyWIP  from @tbl1 
--ORDER BY CreateDate

 

我想先从 StockQty 分配 BalanceQty,当 StockQty 完成后再从 WIPQty 分配

这工作正常(下面只是为了理解)。

对于第一行 BalanceQty = 10,StockQty=10 所以如果我们从余额中分配所有 10 个数量,即 StockAllocateQty = 10 和 UpdatedStockQty = 0。(余额 - StockQty)= UpdatedStockQty

对于第 2 行 BalanceQty = 5,StockQty = 0(所有库存数量都在第 1 行使用)所以如果我们从余额中分配 5 个数量,那么我们将得到 StockAllocateQty = 5 和 UpdatedStockQty = -5。 (0 - 5) = -5

对于第 3 行 BalanceQty = 2,StockQty = 0(所有库存数量都在第 1 行使用)所以如果我们从余额中分配 2 个数量,那么我们将得到 StockAllocateQty = -7 和 UpdatedStockQty = -5。 (-5 - -2) = -7

问题出在这 2 列 AllocateQtyWIP UpdatedStockQtyWIP

现在我有额外的工作正在进行 如果第一行使用了所有库存数量,则分配数量 所有库存数量都被使用,所以我分配给第二行

对于第一行,我们使用的是库存数量,我们甚至没有使用 WIP 数量,它应该是 AllocateQtyWIP = 0 UpdatedStockQtyWIP = 40 但我得到 AllocateQtyWIP = 10,UpdatedStockQtyWIP = 0

这两列的预期输出:

AllocateQtyWIP UpdatedStockQtyWIP
0 40
5 35
2 33

但我得到的是:

ItemId SOQty DIQty IssueQty BalanceQty CreateDate StockQty WIPQty StockAllocateQty UpdatedStockQty AllocateQtyWIP UpdatedStockQtyWIP
1 10 10 0 10 2021-12-16 19:28:32.200 10 0 10 0 10 0
1 15 10 10 5 2021-12-18 19:28:34.200 0 40 5 -5 5 35
1 8 5 2 2 2021-12-19 19:28:35.200 0 0 2 -7 2 33

期待这个

ItemId SOQty DIQty IssueQty BalanceQty CreateDate StockQty WIPQty StockAllocateQty UpdatedStockQty AllocateQtyWIP UpdatedStockQtyWIP
1 10 10 0 10 2021-12-16 19:28:32.200 10 0 10 0 0 40
1 15 10 10 5 2021-12-18 19:28:34.200 0 40 5 -5 5 35
1 8 5 2 2 2021-12-19 19:28:35.200 0 0 2 -7 2 33
 declare @tbl as table
(
 ItemId int,  
 BalanceQty int,
 CreateDate datetime,
 StockQty int,
 WIPQty int
)


insert into @tbl values 
(1,10,'2021-12-16 19:28:32.200',30,0), 
(1,5,'2021-12-18 19:28:34.200',30,0),
(1,2,'2021-12-19 19:28:35.200',30,0)
  
update x set x.StockQty = tx.StockQty  from @tbl x
join 
(select * from 
(
select *,ROW_NUMBER()over(partition by itemid order by CreateDate) as RowNo from @tbl  
)as t where t.RowNo = 1) as tx on tx.CreateDate = x.CreateDate
 

 update x set x.StockQty = 0 from @tbl x
join 
(select * from 
(
select *,ROW_NUMBER()over(partition by itemid order by CreateDate) as RowNo from @tbl  
)as t where t.RowNo != 1) as tx on tx.CreateDate = x.CreateDate
 

 declare @tbl1 as table
(
 ItemId int, 
 BalanceQty int,
 CreateDate datetime,
 StockQty int,
 WIPQty int,
 allocateQTy int,
 UpdatedStockQty int,
 WIPQty1 int
)
 
 insert into @tbl1
select *,BalanceQty as allocateQTy ,SUM(StockQty - BalanceQty) 
over(partition by ItemId order by CreateDate   Rows BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) as UpdatedStockQty,40  from @tbl 
 
 
 declare @tblWIPQty table
 (
 ItemId int,
 StockQty int
 )

insert into @tblWIPQty values(1,40)
 
declare @tbl2 as table
(
ItemId int, 
BalanceQty int,
CreateDate datetime,
StockQty int,
WIPQty int,
allocateQTy int,
UpdatedStockQty int,
WIPQty1 int,
allocateQTyWIP int,
UpdatedStockQtyWIP int
 
)
  

 update x set x.WIPQty =  tt.StockQty  from @tbl1 x
join 
(select * from  
(
select top 1 * from @tbl1 where UpdatedStockQty < 0
) as t) as t on t.CreateDate = x.CreateDate 
join @tblWIPQty tt on tt.ItemId = x.ItemId

 
 
insert into @tbl2 
select *,
case when SUM(StockQty - BalanceQty) over(partition by ItemId order by CreateDate   Rows BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) >= 0 then 0 else BalanceQty end  as AllocateQtyWIP ,
case when SUM(StockQty - BalanceQty) over(partition by ItemId order by CreateDate   Rows BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) < 0 then SUM(case when StockQty - BalanceQty >= 0 then StockQty - BalanceQty else WIPQty - BalanceQty end) over(partition by ItemId order by CreateDate   Rows BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) else WIPQty1 end as UpdatedStockQtyWIP   from @tbl1 
 
 
select * from @tbl2

我自己解决的谢谢大家的支持